blob: a42e6aec4aadb3ef32b36378910af0ba6f1d6cbb [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
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324/*********************************************************************
325 * SYSFS INTERFACE *
326 *********************************************************************/
327
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700328static struct cpufreq_governor *__find_governor(const char *str_governor)
329{
330 struct cpufreq_governor *t;
331
332 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
Dave Jones29464f22009-01-18 01:37:11 -0500333 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700334 return t;
335
336 return NULL;
337}
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339/**
340 * cpufreq_parse_governor - parse a governor string
341 */
Dave Jones905d77c2008-03-05 14:28:32 -0500342static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 struct cpufreq_governor **governor)
344{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700345 int err = -EINVAL;
346
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200347 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700348 goto out;
349
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200350 if (cpufreq_driver->setpolicy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
352 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700353 err = 0;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530354 } else if (!strnicmp(str_governor, "powersave",
355 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700357 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530359 } else if (has_target()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700361
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800362 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700363
364 t = __find_governor(str_governor);
365
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700366 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700367 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700368
Kees Cook1a8e1462011-05-04 08:38:56 -0700369 mutex_unlock(&cpufreq_governor_mutex);
370 ret = request_module("cpufreq_%s", str_governor);
371 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700372
Kees Cook1a8e1462011-05-04 08:38:56 -0700373 if (ret == 0)
374 t = __find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700375 }
376
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700377 if (t != NULL) {
378 *governor = t;
379 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700381
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800382 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
Dave Jones29464f22009-01-18 01:37:11 -0500384out:
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700385 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530389 * cpufreq_per_cpu_attr_read() / show_##file_name() -
390 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 *
392 * Write out information from cpufreq_driver->policy[cpu]; object must be
393 * "unsigned int".
394 */
395
Dave Jones32ee8c32006-02-28 00:43:23 -0500396#define show_one(file_name, object) \
397static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500398(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500399{ \
Dave Jones29464f22009-01-18 01:37:11 -0500400 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
403show_one(cpuinfo_min_freq, cpuinfo.min_freq);
404show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100405show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406show_one(scaling_min_freq, min);
407show_one(scaling_max_freq, max);
408show_one(scaling_cur_freq, cur);
409
Viresh Kumar037ce832013-10-02 14:13:16 +0530410static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530411 struct cpufreq_policy *new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413/**
414 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
415 */
416#define store_one(file_name, object) \
417static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500418(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{ \
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530420 int ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 struct cpufreq_policy new_policy; \
422 \
423 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
424 if (ret) \
425 return -EINVAL; \
426 \
Dave Jones29464f22009-01-18 01:37:11 -0500427 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (ret != 1) \
429 return -EINVAL; \
430 \
Viresh Kumar037ce832013-10-02 14:13:16 +0530431 ret = cpufreq_set_policy(policy, &new_policy); \
Thomas Renninger7970e082006-04-13 15:14:04 +0200432 policy->user_policy.object = policy->object; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 \
434 return ret ? ret : count; \
435}
436
Dave Jones29464f22009-01-18 01:37:11 -0500437store_one(scaling_min_freq, min);
438store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440/**
441 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
442 */
Dave Jones905d77c2008-03-05 14:28:32 -0500443static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
444 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800446 unsigned int cur_freq = __cpufreq_get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (!cur_freq)
448 return sprintf(buf, "<unknown>");
449 return sprintf(buf, "%u\n", cur_freq);
450}
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452/**
453 * show_scaling_governor - show the current policy for the specified CPU
454 */
Dave Jones905d77c2008-03-05 14:28:32 -0500455static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Dave Jones29464f22009-01-18 01:37:11 -0500457 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return sprintf(buf, "powersave\n");
459 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
460 return sprintf(buf, "performance\n");
461 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200462 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500463 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return -EINVAL;
465}
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467/**
468 * store_scaling_governor - store policy for the specified CPU
469 */
Dave Jones905d77c2008-03-05 14:28:32 -0500470static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
471 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530473 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 char str_governor[16];
475 struct cpufreq_policy new_policy;
476
477 ret = cpufreq_get_policy(&new_policy, policy->cpu);
478 if (ret)
479 return ret;
480
Dave Jones29464f22009-01-18 01:37:11 -0500481 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (ret != 1)
483 return -EINVAL;
484
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530485 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
486 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return -EINVAL;
488
Viresh Kumar037ce832013-10-02 14:13:16 +0530489 ret = cpufreq_set_policy(policy, &new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200490
491 policy->user_policy.policy = policy->policy;
492 policy->user_policy.governor = policy->governor;
Thomas Renninger7970e082006-04-13 15:14:04 +0200493
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530494 if (ret)
495 return ret;
496 else
497 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
500/**
501 * show_scaling_driver - show the cpufreq driver currently loaded
502 */
Dave Jones905d77c2008-03-05 14:28:32 -0500503static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200505 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
508/**
509 * show_scaling_available_governors - show the available CPUfreq governors
510 */
Dave Jones905d77c2008-03-05 14:28:32 -0500511static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
512 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
514 ssize_t i = 0;
515 struct cpufreq_governor *t;
516
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530517 if (!has_target()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 i += sprintf(buf, "performance powersave");
519 goto out;
520 }
521
522 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
Dave Jones29464f22009-01-18 01:37:11 -0500523 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
524 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200526 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500528out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 i += sprintf(&buf[i], "\n");
530 return i;
531}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700532
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800533ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 ssize_t i = 0;
536 unsigned int cpu;
537
Rusty Russell835481d2009-01-04 05:18:06 -0800538 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (i)
540 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
541 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
542 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500543 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
545 i += sprintf(&buf[i], "\n");
546 return i;
547}
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800548EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700550/**
551 * show_related_cpus - show the CPUs affected by each transition even if
552 * hw coordination is in use
553 */
554static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
555{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800556 return cpufreq_show_cpus(policy->related_cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700557}
558
559/**
560 * show_affected_cpus - show the CPUs affected by each transition
561 */
562static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
563{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800564 return cpufreq_show_cpus(policy->cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700565}
566
Venki Pallipadi9e769882007-10-26 10:18:21 -0700567static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500568 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700569{
570 unsigned int freq = 0;
571 unsigned int ret;
572
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700573 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700574 return -EINVAL;
575
576 ret = sscanf(buf, "%u", &freq);
577 if (ret != 1)
578 return -EINVAL;
579
580 policy->governor->store_setspeed(policy, freq);
581
582 return count;
583}
584
585static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
586{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700587 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700588 return sprintf(buf, "<unsupported>\n");
589
590 return policy->governor->show_setspeed(policy, buf);
591}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Thomas Renningere2f74f32009-11-19 12:31:01 +0100593/**
viresh kumar8bf1ac722012-10-23 01:23:33 +0200594 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100595 */
596static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
597{
598 unsigned int limit;
599 int ret;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200600 if (cpufreq_driver->bios_limit) {
601 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
Thomas Renningere2f74f32009-11-19 12:31:01 +0100602 if (!ret)
603 return sprintf(buf, "%u\n", limit);
604 }
605 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
606}
607
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200608cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
609cpufreq_freq_attr_ro(cpuinfo_min_freq);
610cpufreq_freq_attr_ro(cpuinfo_max_freq);
611cpufreq_freq_attr_ro(cpuinfo_transition_latency);
612cpufreq_freq_attr_ro(scaling_available_governors);
613cpufreq_freq_attr_ro(scaling_driver);
614cpufreq_freq_attr_ro(scaling_cur_freq);
615cpufreq_freq_attr_ro(bios_limit);
616cpufreq_freq_attr_ro(related_cpus);
617cpufreq_freq_attr_ro(affected_cpus);
618cpufreq_freq_attr_rw(scaling_min_freq);
619cpufreq_freq_attr_rw(scaling_max_freq);
620cpufreq_freq_attr_rw(scaling_governor);
621cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Dave Jones905d77c2008-03-05 14:28:32 -0500623static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 &cpuinfo_min_freq.attr,
625 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100626 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 &scaling_min_freq.attr,
628 &scaling_max_freq.attr,
629 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700630 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 &scaling_governor.attr,
632 &scaling_driver.attr,
633 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700634 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 NULL
636};
637
Dave Jones29464f22009-01-18 01:37:11 -0500638#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
639#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Dave Jones29464f22009-01-18 01:37:11 -0500641static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Dave Jones905d77c2008-03-05 14:28:32 -0500643 struct cpufreq_policy *policy = to_policy(kobj);
644 struct freq_attr *fattr = to_attr(attr);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530645 ssize_t ret;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530646
647 if (!down_read_trylock(&cpufreq_rwsem))
Viresh Kumar1b750e32013-10-02 14:13:09 +0530648 return -EINVAL;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800649
viresh kumarad7722d2013-10-18 19:10:15 +0530650 down_read(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800651
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530652 if (fattr->show)
653 ret = fattr->show(policy, buf);
654 else
655 ret = -EIO;
656
viresh kumarad7722d2013-10-18 19:10:15 +0530657 up_read(&policy->rwsem);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530658 up_read(&cpufreq_rwsem);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return ret;
661}
662
Dave Jones905d77c2008-03-05 14:28:32 -0500663static ssize_t store(struct kobject *kobj, struct attribute *attr,
664 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Dave Jones905d77c2008-03-05 14:28:32 -0500666 struct cpufreq_policy *policy = to_policy(kobj);
667 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500668 ssize_t ret = -EINVAL;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530669
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530670 get_online_cpus();
671
672 if (!cpu_online(policy->cpu))
673 goto unlock;
674
Viresh Kumar6eed9402013-08-06 22:53:11 +0530675 if (!down_read_trylock(&cpufreq_rwsem))
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530676 goto unlock;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800677
viresh kumarad7722d2013-10-18 19:10:15 +0530678 down_write(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800679
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530680 if (fattr->store)
681 ret = fattr->store(policy, buf, count);
682 else
683 ret = -EIO;
684
viresh kumarad7722d2013-10-18 19:10:15 +0530685 up_write(&policy->rwsem);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530686
Viresh Kumar6eed9402013-08-06 22:53:11 +0530687 up_read(&cpufreq_rwsem);
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530688unlock:
689 put_online_cpus();
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return ret;
692}
693
Dave Jones905d77c2008-03-05 14:28:32 -0500694static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Dave Jones905d77c2008-03-05 14:28:32 -0500696 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200697 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 complete(&policy->kobj_unregister);
699}
700
Emese Revfy52cf25d2010-01-19 02:58:23 +0100701static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 .show = show,
703 .store = store,
704};
705
706static struct kobj_type ktype_cpufreq = {
707 .sysfs_ops = &sysfs_ops,
708 .default_attrs = default_attrs,
709 .release = cpufreq_sysfs_release,
710};
711
Viresh Kumar2361be22013-05-17 16:09:09 +0530712struct kobject *cpufreq_global_kobject;
713EXPORT_SYMBOL(cpufreq_global_kobject);
714
715static int cpufreq_global_kobject_usage;
716
717int cpufreq_get_global_kobject(void)
718{
719 if (!cpufreq_global_kobject_usage++)
720 return kobject_add(cpufreq_global_kobject,
721 &cpu_subsys.dev_root->kobj, "%s", "cpufreq");
722
723 return 0;
724}
725EXPORT_SYMBOL(cpufreq_get_global_kobject);
726
727void cpufreq_put_global_kobject(void)
728{
729 if (!--cpufreq_global_kobject_usage)
730 kobject_del(cpufreq_global_kobject);
731}
732EXPORT_SYMBOL(cpufreq_put_global_kobject);
733
734int cpufreq_sysfs_create_file(const struct attribute *attr)
735{
736 int ret = cpufreq_get_global_kobject();
737
738 if (!ret) {
739 ret = sysfs_create_file(cpufreq_global_kobject, attr);
740 if (ret)
741 cpufreq_put_global_kobject();
742 }
743
744 return ret;
745}
746EXPORT_SYMBOL(cpufreq_sysfs_create_file);
747
748void cpufreq_sysfs_remove_file(const struct attribute *attr)
749{
750 sysfs_remove_file(cpufreq_global_kobject, attr);
751 cpufreq_put_global_kobject();
752}
753EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
754
Dave Jones19d6f7e2009-07-08 17:35:39 -0400755/* symlink affected CPUs */
Viresh Kumar308b60e2013-07-31 14:35:14 +0200756static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400757{
758 unsigned int j;
759 int ret = 0;
760
761 for_each_cpu(j, policy->cpus) {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800762 struct device *cpu_dev;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400763
Viresh Kumar308b60e2013-07-31 14:35:14 +0200764 if (j == policy->cpu)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400765 continue;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400766
Viresh Kumare8fdde12013-07-31 14:31:33 +0200767 pr_debug("Adding link for CPU: %u\n", j);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800768 cpu_dev = get_cpu_device(j);
769 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Dave Jones19d6f7e2009-07-08 17:35:39 -0400770 "cpufreq");
Rafael J. Wysocki71c34612013-08-04 01:19:34 +0200771 if (ret)
772 break;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400773 }
774 return ret;
775}
776
Viresh Kumar308b60e2013-07-31 14:35:14 +0200777static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800778 struct device *dev)
Dave Jones909a6942009-07-08 18:05:42 -0400779{
780 struct freq_attr **drv_attr;
Dave Jones909a6942009-07-08 18:05:42 -0400781 int ret = 0;
Dave Jones909a6942009-07-08 18:05:42 -0400782
783 /* prepare interface data */
784 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800785 &dev->kobj, "cpufreq");
Dave Jones909a6942009-07-08 18:05:42 -0400786 if (ret)
787 return ret;
788
789 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200790 drv_attr = cpufreq_driver->attr;
Dave Jones909a6942009-07-08 18:05:42 -0400791 while ((drv_attr) && (*drv_attr)) {
792 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
793 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200794 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400795 drv_attr++;
796 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200797 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -0400798 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
799 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200800 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400801 }
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530802 if (has_target()) {
Dave Jones909a6942009-07-08 18:05:42 -0400803 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
804 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200805 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400806 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200807 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +0100808 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
809 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200810 goto err_out_kobj_put;
Thomas Renningere2f74f32009-11-19 12:31:01 +0100811 }
Dave Jones909a6942009-07-08 18:05:42 -0400812
Viresh Kumar308b60e2013-07-31 14:35:14 +0200813 ret = cpufreq_add_dev_symlink(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400814 if (ret)
815 goto err_out_kobj_put;
816
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530817 return ret;
818
819err_out_kobj_put:
820 kobject_put(&policy->kobj);
821 wait_for_completion(&policy->kobj_unregister);
822 return ret;
823}
824
825static void cpufreq_init_policy(struct cpufreq_policy *policy)
826{
827 struct cpufreq_policy new_policy;
828 int ret = 0;
829
Viresh Kumard5b73cd2013-08-06 22:53:06 +0530830 memcpy(&new_policy, policy, sizeof(*policy));
Jason Barona27a9ab2013-12-19 22:50:50 +0000831
832 /* Use the default policy if its valid. */
833 if (cpufreq_driver->setpolicy)
834 cpufreq_parse_governor(policy->governor->name,
835 &new_policy.policy, NULL);
836
Viresh Kumar037ce832013-10-02 14:13:16 +0530837 /* assure that the starting sequence is run in cpufreq_set_policy */
Dave Jonesecf7e462009-07-08 18:48:47 -0400838 policy->governor = NULL;
839
840 /* set default policy */
Viresh Kumar037ce832013-10-02 14:13:16 +0530841 ret = cpufreq_set_policy(policy, &new_policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400842 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200843 pr_debug("setting policy failed\n");
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200844 if (cpufreq_driver->exit)
845 cpufreq_driver->exit(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400846 }
Dave Jones909a6942009-07-08 18:05:42 -0400847}
848
Viresh Kumarfcf80582013-01-29 14:39:08 +0000849#ifdef CONFIG_HOTPLUG_CPU
Viresh Kumard8d3b472013-08-04 01:20:07 +0200850static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,
Viresh Kumar42f921a2013-12-20 21:26:02 +0530851 unsigned int cpu, struct device *dev)
Viresh Kumarfcf80582013-01-29 14:39:08 +0000852{
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530853 int ret = 0;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000854 unsigned long flags;
855
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530856 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +0530857 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
858 if (ret) {
859 pr_err("%s: Failed to stop governor\n", __func__);
860 return ret;
861 }
862 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000863
viresh kumarad7722d2013-10-18 19:10:15 +0530864 down_write(&policy->rwsem);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530865
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000866 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530867
Viresh Kumarfcf80582013-01-29 14:39:08 +0000868 cpumask_set_cpu(cpu, policy->cpus);
869 per_cpu(cpufreq_cpu_data, cpu) = policy;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000870 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000871
viresh kumarad7722d2013-10-18 19:10:15 +0530872 up_write(&policy->rwsem);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530873
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530874 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +0530875 if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
876 (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
877 pr_err("%s: Failed to start governor\n", __func__);
878 return ret;
879 }
Viresh Kumar820c6ca2013-04-22 00:48:03 +0200880 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000881
Viresh Kumar42f921a2013-12-20 21:26:02 +0530882 return sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
Viresh Kumarfcf80582013-01-29 14:39:08 +0000883}
884#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530886static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu)
887{
888 struct cpufreq_policy *policy;
889 unsigned long flags;
890
Lan Tianyu44871c92013-09-11 15:05:05 +0800891 read_lock_irqsave(&cpufreq_driver_lock, flags);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530892
893 policy = per_cpu(cpufreq_cpu_data_fallback, cpu);
894
Lan Tianyu44871c92013-09-11 15:05:05 +0800895 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530896
897 return policy;
898}
899
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +0530900static struct cpufreq_policy *cpufreq_policy_alloc(void)
901{
902 struct cpufreq_policy *policy;
903
904 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
905 if (!policy)
906 return NULL;
907
908 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
909 goto err_free_policy;
910
911 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
912 goto err_free_cpumask;
913
Lukasz Majewskic88a1f82013-08-06 22:53:08 +0530914 INIT_LIST_HEAD(&policy->policy_list);
viresh kumarad7722d2013-10-18 19:10:15 +0530915 init_rwsem(&policy->rwsem);
916
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +0530917 return policy;
918
919err_free_cpumask:
920 free_cpumask_var(policy->cpus);
921err_free_policy:
922 kfree(policy);
923
924 return NULL;
925}
926
Viresh Kumar42f921a2013-12-20 21:26:02 +0530927static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
928{
929 struct kobject *kobj;
930 struct completion *cmp;
931
932 down_read(&policy->rwsem);
933 kobj = &policy->kobj;
934 cmp = &policy->kobj_unregister;
935 up_read(&policy->rwsem);
936 kobject_put(kobj);
937
938 /*
939 * We need to make sure that the underlying kobj is
940 * actually not referenced anymore by anybody before we
941 * proceed with unloading.
942 */
943 pr_debug("waiting for dropping of refcount\n");
944 wait_for_completion(cmp);
945 pr_debug("wait complete\n");
946}
947
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +0530948static void cpufreq_policy_free(struct cpufreq_policy *policy)
949{
950 free_cpumask_var(policy->related_cpus);
951 free_cpumask_var(policy->cpus);
952 kfree(policy);
953}
954
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +0530955static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
956{
Srivatsa S. Bhat99ec8992013-09-12 17:29:09 +0530957 if (WARN_ON(cpu == policy->cpu))
Srivatsa S. Bhatcb38ed52013-09-12 01:43:42 +0530958 return;
959
viresh kumarad7722d2013-10-18 19:10:15 +0530960 down_write(&policy->rwsem);
Viresh Kumar8efd5762013-09-17 10:22:11 +0530961
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +0530962 policy->last_cpu = policy->cpu;
963 policy->cpu = cpu;
964
viresh kumarad7722d2013-10-18 19:10:15 +0530965 up_write(&policy->rwsem);
Viresh Kumar8efd5762013-09-17 10:22:11 +0530966
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +0530967 cpufreq_frequency_table_update_policy_cpu(policy);
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +0530968 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
969 CPUFREQ_UPDATE_POLICY_CPU, policy);
970}
971
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +0530972static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif,
973 bool frozen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
Viresh Kumarfcf80582013-01-29 14:39:08 +0000975 unsigned int j, cpu = dev->id;
Viresh Kumar65922462013-02-07 10:56:03 +0530976 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 unsigned long flags;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500979#ifdef CONFIG_HOTPLUG_CPU
Viresh Kumar1b274292013-08-20 12:08:26 +0530980 struct cpufreq_policy *tpolicy;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000981 struct cpufreq_governor *gov;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500982#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Ashok Rajc32b6b82005-10-30 14:59:54 -0800984 if (cpu_is_offline(cpu))
985 return 0;
986
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200987 pr_debug("adding CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989#ifdef CONFIG_SMP
990 /* check whether a different CPU already registered this
991 * CPU because it is in the same boat. */
992 policy = cpufreq_cpu_get(cpu);
993 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -0500994 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 return 0;
996 }
Li Zhong5025d622013-08-21 01:31:08 +0200997#endif
Viresh Kumarfcf80582013-01-29 14:39:08 +0000998
Viresh Kumar6eed9402013-08-06 22:53:11 +0530999 if (!down_read_trylock(&cpufreq_rwsem))
1000 return 0;
1001
Viresh Kumarfcf80582013-01-29 14:39:08 +00001002#ifdef CONFIG_HOTPLUG_CPU
1003 /* Check if this cpu was hot-unplugged earlier and has siblings */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001004 read_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar1b274292013-08-20 12:08:26 +05301005 list_for_each_entry(tpolicy, &cpufreq_policy_list, policy_list) {
1006 if (cpumask_test_cpu(cpu, tpolicy->related_cpus)) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001007 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301008 ret = cpufreq_add_policy_cpu(tpolicy, cpu, dev);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301009 up_read(&cpufreq_rwsem);
1010 return ret;
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301011 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001012 }
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001013 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +00001014#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001016 /*
1017 * Restore the saved policy when doing light-weight init and fall back
1018 * to the full init if that fails.
1019 */
1020 policy = frozen ? cpufreq_policy_restore(cpu) : NULL;
1021 if (!policy) {
1022 frozen = false;
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301023 policy = cpufreq_policy_alloc();
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001024 if (!policy)
1025 goto nomem_out;
1026 }
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301027
1028 /*
1029 * In the resume path, since we restore a saved policy, the assignment
1030 * to policy->cpu is like an update of the existing policy, rather than
1031 * the creation of a brand new one. So we need to perform this update
1032 * by invoking update_policy_cpu().
1033 */
1034 if (frozen && cpu != policy->cpu)
1035 update_policy_cpu(policy, cpu);
1036 else
1037 policy->cpu = cpu;
1038
Viresh Kumar65922462013-02-07 10:56:03 +05301039 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Rusty Russell835481d2009-01-04 05:18:06 -08001040 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 init_completion(&policy->kobj_unregister);
David Howells65f27f32006-11-22 14:55:48 +00001043 INIT_WORK(&policy->update, handle_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045 /* call driver. From then on the cpufreq must be able
1046 * to accept all calls to ->verify and ->setpolicy for this CPU
1047 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001048 ret = cpufreq_driver->init(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001050 pr_debug("initialization failed\n");
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301051 goto err_set_policy_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001053
Viresh Kumarda60ce92013-10-03 20:28:30 +05301054 if (cpufreq_driver->get) {
1055 policy->cur = cpufreq_driver->get(policy->cpu);
1056 if (!policy->cur) {
1057 pr_err("%s: ->get() failed\n", __func__);
1058 goto err_get_freq;
1059 }
1060 }
1061
Viresh Kumarfcf80582013-01-29 14:39:08 +00001062 /* related cpus should atleast have policy->cpus */
1063 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
1064
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001065 /*
1066 * affected cpus must always be the one, which are online. We aren't
1067 * managing offline cpus here.
1068 */
1069 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1070
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301071 if (!frozen) {
1072 policy->user_policy.min = policy->min;
1073 policy->user_policy.max = policy->max;
1074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Thomas Renningera1531ac2008-07-29 22:32:58 -07001076 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1077 CPUFREQ_START, policy);
1078
Viresh Kumarfcf80582013-01-29 14:39:08 +00001079#ifdef CONFIG_HOTPLUG_CPU
1080 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
1081 if (gov) {
1082 policy->governor = gov;
1083 pr_debug("Restoring governor %s for cpu %d\n",
1084 policy->governor->name, cpu);
Thomas Renninger4bfa0422009-07-24 15:25:03 +02001085 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001086#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301088 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar474deff2013-08-20 12:08:25 +05301089 for_each_cpu(j, policy->cpus)
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301090 per_cpu(cpufreq_cpu_data, j) = policy;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301091 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1092
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301093 if (!frozen) {
Viresh Kumar308b60e2013-07-31 14:35:14 +02001094 ret = cpufreq_add_dev_interface(policy, dev);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301095 if (ret)
1096 goto err_out_unregister;
1097 }
Dave Jones8ff69732006-03-05 03:37:23 -05001098
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301099 write_lock_irqsave(&cpufreq_driver_lock, flags);
1100 list_add(&policy->policy_list, &cpufreq_policy_list);
1101 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1102
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301103 cpufreq_init_policy(policy);
1104
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301105 if (!frozen) {
1106 policy->user_policy.policy = policy->policy;
1107 policy->user_policy.governor = policy->governor;
1108 }
1109
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001110 kobject_uevent(&policy->kobj, KOBJ_ADD);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301111 up_read(&cpufreq_rwsem);
1112
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001113 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 return 0;
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117err_out_unregister:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001118 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar474deff2013-08-20 12:08:25 +05301119 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -07001120 per_cpu(cpufreq_cpu_data, j) = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001121 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Viresh Kumarda60ce92013-10-03 20:28:30 +05301123err_get_freq:
1124 if (cpufreq_driver->exit)
1125 cpufreq_driver->exit(policy);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301126err_set_policy_cpu:
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001127 if (frozen) {
1128 /* Do not leave stale fallback data behind. */
1129 per_cpu(cpufreq_cpu_data_fallback, cpu) = NULL;
Viresh Kumar42f921a2013-12-20 21:26:02 +05301130 cpufreq_policy_put_kobj(policy);
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001131 }
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301132 cpufreq_policy_free(policy);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134nomem_out:
Viresh Kumar6eed9402013-08-06 22:53:11 +05301135 up_read(&cpufreq_rwsem);
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 return ret;
1138}
1139
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301140/**
1141 * cpufreq_add_dev - add a CPU device
1142 *
1143 * Adds the cpufreq interface for a CPU device.
1144 *
1145 * The Oracle says: try running cpufreq registration/unregistration concurrently
1146 * with with cpu hotplugging and all hell will break loose. Tried to clean this
1147 * mess up, but more thorough testing is needed. - Mathieu
1148 */
1149static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1150{
1151 return __cpufreq_add_dev(dev, sif, false);
1152}
1153
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301154static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy *policy,
Viresh Kumar42f921a2013-12-20 21:26:02 +05301155 unsigned int old_cpu)
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301156{
1157 struct device *cpu_dev;
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301158 int ret;
1159
1160 /* first sibling now owns the new sysfs dir */
Viresh Kumar9c8f1ee2013-09-12 17:06:33 +05301161 cpu_dev = get_cpu_device(cpumask_any_but(policy->cpus, old_cpu));
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301162
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301163 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301164 ret = kobject_move(&policy->kobj, &cpu_dev->kobj);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301165 if (ret) {
1166 pr_err("%s: Failed to move kobj: %d", __func__, ret);
1167
viresh kumarad7722d2013-10-18 19:10:15 +05301168 down_write(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301169 cpumask_set_cpu(old_cpu, policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301170 up_write(&policy->rwsem);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301171
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301172 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301173 "cpufreq");
1174
1175 return -EINVAL;
1176 }
1177
1178 return cpu_dev->id;
1179}
1180
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301181static int __cpufreq_remove_dev_prepare(struct device *dev,
1182 struct subsys_interface *sif,
1183 bool frozen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301185 unsigned int cpu = dev->id, cpus;
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301186 int new_cpu, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 unsigned long flags;
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301188 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001190 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001192 write_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301194 policy = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301196 /* Save the policy somewhere when doing a light-weight tear-down */
1197 if (frozen)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301198 per_cpu(cpufreq_cpu_data_fallback, cpu) = policy;
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301199
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001200 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301202 if (!policy) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001203 pr_debug("%s: No cpu_data found\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301207 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301208 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1209 if (ret) {
1210 pr_err("%s: Failed to stop governor\n", __func__);
1211 return ret;
1212 }
1213 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001214
Jacob Shin27ecddc2011-04-27 13:32:11 -05001215#ifdef CONFIG_HOTPLUG_CPU
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001216 if (!cpufreq_driver->setpolicy)
Dirk Brandewiefa69e332013-02-06 09:02:11 -08001217 strncpy(per_cpu(cpufreq_cpu_governor, cpu),
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301218 policy->governor->name, CPUFREQ_NAME_LEN);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001219#endif
1220
viresh kumarad7722d2013-10-18 19:10:15 +05301221 down_read(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301222 cpus = cpumask_weight(policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301223 up_read(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Srivatsa S. Bhat61173f22013-09-12 01:43:25 +05301225 if (cpu != policy->cpu) {
1226 if (!frozen)
1227 sysfs_remove_link(&dev->kobj, "cpufreq");
Viresh Kumar73bf0fc2013-02-05 22:21:14 +01001228 } else if (cpus > 1) {
Viresh Kumar42f921a2013-12-20 21:26:02 +05301229 new_cpu = cpufreq_nominate_new_policy_cpu(policy, cpu);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301230 if (new_cpu >= 0) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301231 update_policy_cpu(policy, new_cpu);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301232
1233 if (!frozen) {
Viresh Kumar75949c92013-10-02 14:13:13 +05301234 pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
1235 __func__, new_cpu, cpu);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301236 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001237 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001238 }
Venki Pallipadiec282972007-03-26 12:03:19 -07001239
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301240 return 0;
1241}
1242
1243static int __cpufreq_remove_dev_finish(struct device *dev,
1244 struct subsys_interface *sif,
1245 bool frozen)
1246{
1247 unsigned int cpu = dev->id, cpus;
1248 int ret;
1249 unsigned long flags;
1250 struct cpufreq_policy *policy;
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301251
1252 read_lock_irqsave(&cpufreq_driver_lock, flags);
1253 policy = per_cpu(cpufreq_cpu_data, cpu);
1254 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1255
1256 if (!policy) {
1257 pr_debug("%s: No cpu_data found\n", __func__);
1258 return -EINVAL;
1259 }
1260
viresh kumarad7722d2013-10-18 19:10:15 +05301261 down_write(&policy->rwsem);
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301262 cpus = cpumask_weight(policy->cpus);
Viresh Kumar9c8f1ee2013-09-12 17:06:33 +05301263
1264 if (cpus > 1)
1265 cpumask_clear_cpu(cpu, policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301266 up_write(&policy->rwsem);
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301267
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001268 /* If cpu is last user of policy, free policy */
1269 if (cpus == 1) {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301270 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301271 ret = __cpufreq_governor(policy,
1272 CPUFREQ_GOV_POLICY_EXIT);
1273 if (ret) {
1274 pr_err("%s: Failed to exit governor\n",
1275 __func__);
1276 return ret;
1277 }
Viresh Kumaredab2fb2013-08-20 12:08:22 +05301278 }
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001279
Viresh Kumar42f921a2013-12-20 21:26:02 +05301280 if (!frozen)
1281 cpufreq_policy_put_kobj(policy);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301282
1283 /*
1284 * Perform the ->exit() even during light-weight tear-down,
1285 * since this is a core component, and is essential for the
1286 * subsequent light-weight ->init() to succeed.
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001287 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001288 if (cpufreq_driver->exit)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301289 cpufreq_driver->exit(policy);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001290
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301291 /* Remove policy from list of active policies */
1292 write_lock_irqsave(&cpufreq_driver_lock, flags);
1293 list_del(&policy->policy_list);
1294 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1295
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301296 if (!frozen)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301297 cpufreq_policy_free(policy);
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001298 } else {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301299 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301300 if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
1301 (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
1302 pr_err("%s: Failed to start governor\n",
1303 __func__);
1304 return ret;
1305 }
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001306 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Viresh Kumar474deff2013-08-20 12:08:25 +05301309 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 return 0;
1311}
1312
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301313/**
Viresh Kumar27a862e2013-10-02 14:13:14 +05301314 * cpufreq_remove_dev - remove a CPU device
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301315 *
1316 * Removes the cpufreq interface for a CPU device.
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301317 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001318static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001319{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001320 unsigned int cpu = dev->id;
Viresh Kumar27a862e2013-10-02 14:13:14 +05301321 int ret;
Venki Pallipadiec282972007-03-26 12:03:19 -07001322
1323 if (cpu_is_offline(cpu))
1324 return 0;
1325
Viresh Kumar27a862e2013-10-02 14:13:14 +05301326 ret = __cpufreq_remove_dev_prepare(dev, sif, false);
1327
1328 if (!ret)
1329 ret = __cpufreq_remove_dev_finish(dev, sif, false);
1330
1331 return ret;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001332}
1333
David Howells65f27f32006-11-22 14:55:48 +00001334static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
David Howells65f27f32006-11-22 14:55:48 +00001336 struct cpufreq_policy *policy =
1337 container_of(work, struct cpufreq_policy, update);
1338 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001339 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 cpufreq_update_policy(cpu);
1341}
1342
1343/**
Viresh Kumarbb176f72013-06-19 14:19:33 +05301344 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1345 * in deep trouble.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 * @cpu: cpu number
1347 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1348 * @new_freq: CPU frequency the CPU actually runs at
1349 *
Dave Jones29464f22009-01-18 01:37:11 -05001350 * We adjust to current frequency first, and need to clean up later.
1351 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301353static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1354 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301356 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301358 unsigned long flags;
1359
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001360 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 freqs.old = old_freq;
1364 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301365
1366 read_lock_irqsave(&cpufreq_driver_lock, flags);
1367 policy = per_cpu(cpufreq_cpu_data, cpu);
1368 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1369
1370 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1371 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372}
1373
Dave Jones32ee8c32006-02-28 00:43:23 -05001374/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301375 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001376 * @cpu: CPU number
1377 *
1378 * This is the last known freq, without actually getting it from the driver.
1379 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1380 */
1381unsigned int cpufreq_quick_get(unsigned int cpu)
1382{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001383 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301384 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001385
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001386 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1387 return cpufreq_driver->get(cpu);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001388
1389 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001390 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301391 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001392 cpufreq_cpu_put(policy);
1393 }
1394
Dave Jones4d34a672008-02-07 16:33:49 -05001395 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001396}
1397EXPORT_SYMBOL(cpufreq_quick_get);
1398
Jesse Barnes3d737102011-06-28 10:59:12 -07001399/**
1400 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1401 * @cpu: CPU number
1402 *
1403 * Just return the max possible frequency for a given CPU.
1404 */
1405unsigned int cpufreq_quick_get_max(unsigned int cpu)
1406{
1407 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1408 unsigned int ret_freq = 0;
1409
1410 if (policy) {
1411 ret_freq = policy->max;
1412 cpufreq_cpu_put(policy);
1413 }
1414
1415 return ret_freq;
1416}
1417EXPORT_SYMBOL(cpufreq_quick_get_max);
1418
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001419static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001421 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301422 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001424 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001425 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001427 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301429 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001430 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301431 /* verify no discrepancy between actual and
1432 saved value exists */
1433 if (unlikely(ret_freq != policy->cur)) {
1434 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 schedule_work(&policy->update);
1436 }
1437 }
1438
Dave Jones4d34a672008-02-07 16:33:49 -05001439 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001440}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001442/**
1443 * cpufreq_get - get the current CPU frequency (in kHz)
1444 * @cpu: CPU number
1445 *
1446 * Get the CPU current (static) CPU frequency
1447 */
1448unsigned int cpufreq_get(unsigned int cpu)
1449{
viresh kumarad7722d2013-10-18 19:10:15 +05301450 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001451 unsigned int ret_freq = 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001452
Viresh Kumar26ca8692013-09-20 22:37:31 +05301453 if (cpufreq_disabled() || !cpufreq_driver)
1454 return -ENOENT;
1455
viresh kumarad7722d2013-10-18 19:10:15 +05301456 BUG_ON(!policy);
1457
Viresh Kumar6eed9402013-08-06 22:53:11 +05301458 if (!down_read_trylock(&cpufreq_rwsem))
1459 return 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001460
viresh kumarad7722d2013-10-18 19:10:15 +05301461 down_read(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001462
1463 ret_freq = __cpufreq_get(cpu);
1464
viresh kumarad7722d2013-10-18 19:10:15 +05301465 up_read(&policy->rwsem);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301466 up_read(&cpufreq_rwsem);
1467
Dave Jones4d34a672008-02-07 16:33:49 -05001468 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
1470EXPORT_SYMBOL(cpufreq_get);
1471
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001472static struct subsys_interface cpufreq_interface = {
1473 .name = "cpufreq",
1474 .subsys = &cpu_subsys,
1475 .add_dev = cpufreq_add_dev,
1476 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001477};
1478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001480 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1481 *
1482 * This function is only executed for the boot processor. The other CPUs
1483 * have been put offline by means of CPU hotplug.
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001484 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001485static int cpufreq_bp_suspend(void)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001486{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301487 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001488
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001489 int cpu = smp_processor_id();
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301490 struct cpufreq_policy *policy;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001491
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001492 pr_debug("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001493
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001494 /* If there's no policy for the boot CPU, we have nothing to do. */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301495 policy = cpufreq_cpu_get(cpu);
1496 if (!policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001497 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001498
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001499 if (cpufreq_driver->suspend) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301500 ret = cpufreq_driver->suspend(policy);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001501 if (ret)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001502 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301503 "step on CPU %u\n", policy->cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001504 }
1505
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301506 cpufreq_cpu_put(policy);
Dave Jonesc9060492008-02-07 16:32:18 -05001507 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001508}
1509
1510/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001511 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 *
1513 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001514 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1515 * restored. It will verify that the current freq is in sync with
1516 * what we believe it to be. This is a bit later than when it
1517 * should be, but nonethteless it's better than calling
1518 * cpufreq_driver->get() here which might re-enable interrupts...
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001519 *
1520 * This function is only executed for the boot CPU. The other CPUs have not
1521 * been turned on yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001523static void cpufreq_bp_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301525 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001526
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001527 int cpu = smp_processor_id();
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301528 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001530 pr_debug("resuming cpu %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001532 /* If there's no policy for the boot CPU, we have nothing to do. */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301533 policy = cpufreq_cpu_get(cpu);
1534 if (!policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001535 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001537 if (cpufreq_driver->resume) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301538 ret = cpufreq_driver->resume(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 if (ret) {
1540 printk(KERN_ERR "cpufreq: resume failed in ->resume "
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301541 "step on CPU %u\n", policy->cpu);
Dave Jonesc9060492008-02-07 16:32:18 -05001542 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 }
1544 }
1545
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301546 schedule_work(&policy->update);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001547
Dave Jonesc9060492008-02-07 16:32:18 -05001548fail:
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301549 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550}
1551
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001552static struct syscore_ops cpufreq_syscore_ops = {
1553 .suspend = cpufreq_bp_suspend,
1554 .resume = cpufreq_bp_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555};
1556
Borislav Petkov9d950462013-01-20 10:24:28 +00001557/**
1558 * cpufreq_get_current_driver - return current driver's name
1559 *
1560 * Return the name string of the currently loaded cpufreq driver
1561 * or NULL, if none.
1562 */
1563const char *cpufreq_get_current_driver(void)
1564{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001565 if (cpufreq_driver)
1566 return cpufreq_driver->name;
1567
1568 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001569}
1570EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572/*********************************************************************
1573 * NOTIFIER LISTS INTERFACE *
1574 *********************************************************************/
1575
1576/**
1577 * cpufreq_register_notifier - register a driver with cpufreq
1578 * @nb: notifier function to register
1579 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1580 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001581 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 * are notified about clock rate changes (once before and once after
1583 * the transition), or a list of drivers that are notified about
1584 * changes in cpufreq policy.
1585 *
1586 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001587 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 */
1589int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1590{
1591 int ret;
1592
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001593 if (cpufreq_disabled())
1594 return -EINVAL;
1595
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001596 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 switch (list) {
1599 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001600 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001601 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 break;
1603 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001604 ret = blocking_notifier_chain_register(
1605 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 break;
1607 default:
1608 ret = -EINVAL;
1609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
1611 return ret;
1612}
1613EXPORT_SYMBOL(cpufreq_register_notifier);
1614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615/**
1616 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1617 * @nb: notifier block to be unregistered
Viresh Kumarbb176f72013-06-19 14:19:33 +05301618 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 *
1620 * Remove a driver from the CPU frequency notifier list.
1621 *
1622 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001623 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 */
1625int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1626{
1627 int ret;
1628
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001629 if (cpufreq_disabled())
1630 return -EINVAL;
1631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 switch (list) {
1633 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001634 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001635 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 break;
1637 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001638 ret = blocking_notifier_chain_unregister(
1639 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 break;
1641 default:
1642 ret = -EINVAL;
1643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645 return ret;
1646}
1647EXPORT_SYMBOL(cpufreq_unregister_notifier);
1648
1649
1650/*********************************************************************
1651 * GOVERNORS *
1652 *********************************************************************/
1653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654int __cpufreq_driver_target(struct cpufreq_policy *policy,
1655 unsigned int target_freq,
1656 unsigned int relation)
1657{
1658 int retval = -EINVAL;
Viresh Kumar72499242012-10-31 01:28:21 +01001659 unsigned int old_target_freq = target_freq;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001660
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001661 if (cpufreq_disabled())
1662 return -ENODEV;
1663
Viresh Kumar72499242012-10-31 01:28:21 +01001664 /* Make sure that target_freq is within supported range */
1665 if (target_freq > policy->max)
1666 target_freq = policy->max;
1667 if (target_freq < policy->min)
1668 target_freq = policy->min;
1669
1670 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1671 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001672
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301673 /*
1674 * This might look like a redundant call as we are checking it again
1675 * after finding index. But it is left intentionally for cases where
1676 * exactly same freq is called again and so we can save on few function
1677 * calls.
1678 */
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001679 if (target_freq == policy->cur)
1680 return 0;
1681
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001682 if (cpufreq_driver->target)
1683 retval = cpufreq_driver->target(policy, target_freq, relation);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301684 else if (cpufreq_driver->target_index) {
1685 struct cpufreq_frequency_table *freq_table;
Viresh Kumard4019f02013-08-14 19:38:24 +05301686 struct cpufreq_freqs freqs;
1687 bool notify;
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301688 int index;
Ashok Raj90d45d12005-11-08 21:34:24 -08001689
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301690 freq_table = cpufreq_frequency_get_table(policy->cpu);
1691 if (unlikely(!freq_table)) {
1692 pr_err("%s: Unable to find freq_table\n", __func__);
1693 goto out;
1694 }
1695
1696 retval = cpufreq_frequency_table_target(policy, freq_table,
1697 target_freq, relation, &index);
1698 if (unlikely(retval)) {
1699 pr_err("%s: Unable to find matching freq\n", __func__);
1700 goto out;
1701 }
1702
Viresh Kumard4019f02013-08-14 19:38:24 +05301703 if (freq_table[index].frequency == policy->cur) {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301704 retval = 0;
Viresh Kumard4019f02013-08-14 19:38:24 +05301705 goto out;
1706 }
1707
1708 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
1709
1710 if (notify) {
1711 freqs.old = policy->cur;
1712 freqs.new = freq_table[index].frequency;
1713 freqs.flags = 0;
1714
1715 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1716 __func__, policy->cpu, freqs.old,
1717 freqs.new);
1718
1719 cpufreq_notify_transition(policy, &freqs,
1720 CPUFREQ_PRECHANGE);
1721 }
1722
1723 retval = cpufreq_driver->target_index(policy, index);
1724 if (retval)
1725 pr_err("%s: Failed to change cpu frequency: %d\n",
1726 __func__, retval);
1727
1728 if (notify) {
1729 /*
1730 * Notify with old freq in case we failed to change
1731 * frequency
1732 */
1733 if (retval)
1734 freqs.new = freqs.old;
1735
1736 cpufreq_notify_transition(policy, &freqs,
1737 CPUFREQ_POSTCHANGE);
1738 }
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301739 }
1740
1741out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 return retval;
1743}
1744EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746int cpufreq_driver_target(struct cpufreq_policy *policy,
1747 unsigned int target_freq,
1748 unsigned int relation)
1749{
Julia Lawallf1829e42008-07-25 22:44:53 +02001750 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
viresh kumarad7722d2013-10-18 19:10:15 +05301752 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
1754 ret = __cpufreq_driver_target(policy, target_freq, relation);
1755
viresh kumarad7722d2013-10-18 19:10:15 +05301756 up_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 return ret;
1759}
1760EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1761
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001762/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001763 * when "event" is CPUFREQ_GOV_LIMITS
1764 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301766static int __cpufreq_governor(struct cpufreq_policy *policy,
1767 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768{
Dave Jonescc993ca2005-07-28 09:43:56 -07001769 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001770
1771 /* Only must be defined when default governor is known to have latency
1772 restrictions, like e.g. conservative or ondemand.
1773 That this is the case is already ensured in Kconfig
1774 */
1775#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1776 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1777#else
1778 struct cpufreq_governor *gov = NULL;
1779#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001780
1781 if (policy->governor->max_transition_latency &&
1782 policy->cpuinfo.transition_latency >
1783 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07001784 if (!gov)
1785 return -EINVAL;
1786 else {
1787 printk(KERN_WARNING "%s governor failed, too long"
1788 " transition latency of HW, fallback"
1789 " to %s governor\n",
1790 policy->governor->name,
1791 gov->name);
1792 policy->governor = gov;
1793 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Viresh Kumarfe492f32013-08-06 22:53:10 +05301796 if (event == CPUFREQ_GOV_POLICY_INIT)
1797 if (!try_module_get(policy->governor->owner))
1798 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001800 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301801 policy->cpu, event);
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001802
1803 mutex_lock(&cpufreq_governor_lock);
Srivatsa S. Bhat56d07db2013-09-07 01:23:55 +05301804 if ((policy->governor_enabled && event == CPUFREQ_GOV_START)
Viresh Kumarf73d3932013-08-31 17:53:40 +05301805 || (!policy->governor_enabled
1806 && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) {
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001807 mutex_unlock(&cpufreq_governor_lock);
1808 return -EBUSY;
1809 }
1810
1811 if (event == CPUFREQ_GOV_STOP)
1812 policy->governor_enabled = false;
1813 else if (event == CPUFREQ_GOV_START)
1814 policy->governor_enabled = true;
1815
1816 mutex_unlock(&cpufreq_governor_lock);
1817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 ret = policy->governor->governor(policy, event);
1819
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001820 if (!ret) {
1821 if (event == CPUFREQ_GOV_POLICY_INIT)
1822 policy->governor->initialized++;
1823 else if (event == CPUFREQ_GOV_POLICY_EXIT)
1824 policy->governor->initialized--;
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001825 } else {
1826 /* Restore original values */
1827 mutex_lock(&cpufreq_governor_lock);
1828 if (event == CPUFREQ_GOV_STOP)
1829 policy->governor_enabled = true;
1830 else if (event == CPUFREQ_GOV_START)
1831 policy->governor_enabled = false;
1832 mutex_unlock(&cpufreq_governor_lock);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001833 }
Viresh Kumarb3940582013-02-01 05:42:58 +00001834
Viresh Kumarfe492f32013-08-06 22:53:10 +05301835 if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
1836 ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 module_put(policy->governor->owner);
1838
1839 return ret;
1840}
1841
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842int cpufreq_register_governor(struct cpufreq_governor *governor)
1843{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001844 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
1846 if (!governor)
1847 return -EINVAL;
1848
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001849 if (cpufreq_disabled())
1850 return -ENODEV;
1851
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001852 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001853
Viresh Kumarb3940582013-02-01 05:42:58 +00001854 governor->initialized = 0;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001855 err = -EBUSY;
1856 if (__find_governor(governor->name) == NULL) {
1857 err = 0;
1858 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Dave Jones32ee8c32006-02-28 00:43:23 -05001861 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001862 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863}
1864EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1865
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1867{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001868#ifdef CONFIG_HOTPLUG_CPU
1869 int cpu;
1870#endif
1871
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 if (!governor)
1873 return;
1874
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001875 if (cpufreq_disabled())
1876 return;
1877
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001878#ifdef CONFIG_HOTPLUG_CPU
1879 for_each_present_cpu(cpu) {
1880 if (cpu_online(cpu))
1881 continue;
1882 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1883 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1884 }
1885#endif
1886
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001887 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001889 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 return;
1891}
1892EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1893
1894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895/*********************************************************************
1896 * POLICY INTERFACE *
1897 *********************************************************************/
1898
1899/**
1900 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05001901 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1902 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 *
1904 * Reads the current cpufreq policy.
1905 */
1906int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1907{
1908 struct cpufreq_policy *cpu_policy;
1909 if (!policy)
1910 return -EINVAL;
1911
1912 cpu_policy = cpufreq_cpu_get(cpu);
1913 if (!cpu_policy)
1914 return -EINVAL;
1915
Viresh Kumard5b73cd2013-08-06 22:53:06 +05301916 memcpy(policy, cpu_policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
1918 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 return 0;
1920}
1921EXPORT_SYMBOL(cpufreq_get_policy);
1922
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001923/*
Viresh Kumar037ce832013-10-02 14:13:16 +05301924 * policy : current policy.
1925 * new_policy: policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001926 */
Viresh Kumar037ce832013-10-02 14:13:16 +05301927static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301928 struct cpufreq_policy *new_policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001930 int ret = 0, failed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301932 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", new_policy->cpu,
1933 new_policy->min, new_policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Viresh Kumard5b73cd2013-08-06 22:53:06 +05301935 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301937 if (new_policy->min > policy->max || new_policy->max < policy->min) {
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02001938 ret = -EINVAL;
1939 goto error_out;
1940 }
1941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 /* verify the cpu speed can be set within this limit */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301943 ret = cpufreq_driver->verify(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 if (ret)
1945 goto error_out;
1946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001948 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301949 CPUFREQ_ADJUST, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
1951 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001952 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301953 CPUFREQ_INCOMPATIBLE, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Viresh Kumarbb176f72013-06-19 14:19:33 +05301955 /*
1956 * verify the cpu speed can be set within this limit, which might be
1957 * different to the first one
1958 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301959 ret = cpufreq_driver->verify(new_policy);
Alan Sterne041c682006-03-27 01:16:30 -08001960 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001964 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301965 CPUFREQ_NOTIFY, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301967 policy->min = new_policy->min;
1968 policy->max = new_policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001970 pr_debug("new min and max freqs are %u - %u kHz\n",
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301971 policy->min, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001973 if (cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301974 policy->policy = new_policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001975 pr_debug("setting range\n");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301976 ret = cpufreq_driver->setpolicy(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 } else {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301978 if (new_policy->governor != policy->governor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 /* save old, working values */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301980 struct cpufreq_governor *old_gov = policy->governor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001982 pr_debug("governor switch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
1984 /* end old governor */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301985 if (policy->governor) {
1986 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
viresh kumarad7722d2013-10-18 19:10:15 +05301987 up_write(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301988 __cpufreq_governor(policy,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001989 CPUFREQ_GOV_POLICY_EXIT);
viresh kumarad7722d2013-10-18 19:10:15 +05301990 down_write(&policy->rwsem);
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
1993 /* start new governor */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301994 policy->governor = new_policy->governor;
1995 if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
1996 if (!__cpufreq_governor(policy, CPUFREQ_GOV_START)) {
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001997 failed = 0;
Viresh Kumar955ef482013-05-16 05:09:58 +00001998 } else {
viresh kumarad7722d2013-10-18 19:10:15 +05301999 up_write(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302000 __cpufreq_governor(policy,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002001 CPUFREQ_GOV_POLICY_EXIT);
viresh kumarad7722d2013-10-18 19:10:15 +05302002 down_write(&policy->rwsem);
Viresh Kumar955ef482013-05-16 05:09:58 +00002003 }
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002004 }
2005
2006 if (failed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 /* new governor failed, so re-start old one */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002008 pr_debug("starting governor %s failed\n",
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302009 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 if (old_gov) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302011 policy->governor = old_gov;
2012 __cpufreq_governor(policy,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002013 CPUFREQ_GOV_POLICY_INIT);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302014 __cpufreq_governor(policy,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05302015 CPUFREQ_GOV_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 }
2017 ret = -EINVAL;
2018 goto error_out;
2019 }
2020 /* might be a policy change, too, so fall through */
2021 }
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002022 pr_debug("governor: change or update limits\n");
Viresh Kumar3de9bde2013-08-06 22:53:13 +05302023 ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 }
2025
Dave Jones7d5e3502006-02-02 17:03:42 -05002026error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 return ret;
2028}
2029
2030/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
2032 * @cpu: CPU which shall be re-evaluated
2033 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002034 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 * at different times.
2036 */
2037int cpufreq_update_policy(unsigned int cpu)
2038{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302039 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2040 struct cpufreq_policy new_policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02002041 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302043 if (!policy) {
Julia Lawallf1829e42008-07-25 22:44:53 +02002044 ret = -ENODEV;
2045 goto no_policy;
2046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
viresh kumarad7722d2013-10-18 19:10:15 +05302048 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002050 pr_debug("updating policy for CPU %u\n", cpu);
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302051 memcpy(&new_policy, policy, sizeof(*policy));
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302052 new_policy.min = policy->user_policy.min;
2053 new_policy.max = policy->user_policy.max;
2054 new_policy.policy = policy->user_policy.policy;
2055 new_policy.governor = policy->user_policy.governor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Viresh Kumarbb176f72013-06-19 14:19:33 +05302057 /*
2058 * BIOS might change freq behind our back
2059 * -> ask driver for current freq and notify governors about a change
2060 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002061 if (cpufreq_driver->get) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302062 new_policy.cur = cpufreq_driver->get(cpu);
2063 if (!policy->cur) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002064 pr_debug("Driver did not initialize current freq");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302065 policy->cur = new_policy.cur;
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002066 } else {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302067 if (policy->cur != new_policy.cur && has_target())
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302068 cpufreq_out_of_sync(cpu, policy->cur,
2069 new_policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002070 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01002071 }
2072
Viresh Kumar037ce832013-10-02 14:13:16 +05302073 ret = cpufreq_set_policy(policy, &new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
viresh kumarad7722d2013-10-18 19:10:15 +05302075 up_write(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002076
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302077 cpufreq_cpu_put(policy);
Julia Lawallf1829e42008-07-25 22:44:53 +02002078no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 return ret;
2080}
2081EXPORT_SYMBOL(cpufreq_update_policy);
2082
Paul Gortmaker27609842013-06-19 13:54:04 -04002083static int cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002084 unsigned long action, void *hcpu)
2085{
2086 unsigned int cpu = (unsigned long)hcpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002087 struct device *dev;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302088 bool frozen = false;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002089
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002090 dev = get_cpu_device(cpu);
2091 if (dev) {
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302092
Rafael J. Wysockid4faadd2013-12-08 01:23:58 +01002093 if (action & CPU_TASKS_FROZEN)
2094 frozen = true;
2095
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302096 switch (action & ~CPU_TASKS_FROZEN) {
Ashok Rajc32b6b82005-10-30 14:59:54 -08002097 case CPU_ONLINE:
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302098 __cpufreq_add_dev(dev, NULL, frozen);
Srivatsa S. Bhat23d328992013-07-30 04:23:56 +05302099 cpufreq_update_policy(cpu);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002100 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302101
Ashok Rajc32b6b82005-10-30 14:59:54 -08002102 case CPU_DOWN_PREPARE:
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05302103 __cpufreq_remove_dev_prepare(dev, NULL, frozen);
Srivatsa S. Bhat1aee40a2013-09-07 01:23:27 +05302104 break;
2105
2106 case CPU_POST_DEAD:
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05302107 __cpufreq_remove_dev_finish(dev, NULL, frozen);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002108 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302109
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002110 case CPU_DOWN_FAILED:
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302111 __cpufreq_add_dev(dev, NULL, frozen);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002112 break;
2113 }
2114 }
2115 return NOTIFY_OK;
2116}
2117
Neal Buckendahl9c36f742010-06-22 22:02:44 -05002118static struct notifier_block __refdata cpufreq_cpu_notifier = {
Viresh Kumarbb176f72013-06-19 14:19:33 +05302119 .notifier_call = cpufreq_cpu_callback,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002120};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
2122/*********************************************************************
2123 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2124 *********************************************************************/
2125
2126/**
2127 * cpufreq_register_driver - register a CPU Frequency driver
2128 * @driver_data: A struct cpufreq_driver containing the values#
2129 * submitted by the CPU Frequency driver.
2130 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302131 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05002133 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 *
2135 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002136int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137{
2138 unsigned long flags;
2139 int ret;
2140
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002141 if (cpufreq_disabled())
2142 return -ENODEV;
2143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 if (!driver_data || !driver_data->verify || !driver_data->init ||
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302145 !(driver_data->setpolicy || driver_data->target_index ||
2146 driver_data->target))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 return -EINVAL;
2148
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002149 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
2151 if (driver_data->setpolicy)
2152 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2153
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002154 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002155 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002156 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Yinghai Lu4dea58062013-09-18 21:05:20 -07002157 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002159 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002160 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002162 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002163 if (ret)
2164 goto err_null_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002166 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 int i;
2168 ret = -ENODEV;
2169
2170 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07002171 for (i = 0; i < nr_cpu_ids; i++)
2172 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07002174 break;
2175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
2177 /* if all ->init() calls failed, unregister */
2178 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002179 pr_debug("no CPU initialized for driver %s\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05302180 driver_data->name);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002181 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 }
2183 }
2184
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002185 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002186 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002188 return 0;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002189err_if_unreg:
2190 subsys_interface_unregister(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002191err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002192 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002193 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002194 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05002195 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196}
2197EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2198
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199/**
2200 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2201 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302202 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 * the right to do so, i.e. if you have succeeded in initialising before!
2204 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2205 * currently not initialised.
2206 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002207int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
2209 unsigned long flags;
2210
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002211 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002214 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002216 subsys_interface_unregister(&cpufreq_interface);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07002217 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
Viresh Kumar6eed9402013-08-06 22:53:11 +05302219 down_write(&cpufreq_rwsem);
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002220 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302221
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002222 cpufreq_driver = NULL;
Viresh Kumar6eed9402013-08-06 22:53:11 +05302223
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002224 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302225 up_write(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
2227 return 0;
2228}
2229EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002230
2231static int __init cpufreq_core_init(void)
2232{
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002233 if (cpufreq_disabled())
2234 return -ENODEV;
2235
Viresh Kumar2361be22013-05-17 16:09:09 +05302236 cpufreq_global_kobject = kobject_create();
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002237 BUG_ON(!cpufreq_global_kobject);
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01002238 register_syscore_ops(&cpufreq_syscore_ops);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002239
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002240 return 0;
2241}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002242core_initcall(cpufreq_core_init);