blob: 289a407a92078a0f30f61923bfcfd7113d57d20b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/cpufreq/cpufreq.c
3 *
4 * Copyright (C) 2001 Russell King
5 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
Viresh Kumarbb176f72013-06-19 14:19:33 +05306 * (C) 2013 Viresh Kumar <viresh.kumar@linaro.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Ashok Rajc32b6b82005-10-30 14:59:54 -08008 * Oct 2005 - Ashok Raj <ashok.raj@intel.com>
Dave Jones32ee8c32006-02-28 00:43:23 -05009 * Added handling for CPU hotplug
Dave Jones8ff69732006-03-05 03:37:23 -050010 * Feb 2006 - Jacob Shin <jacob.shin@amd.com>
11 * Fix handling for CPU hotplug -- affected CPUs
Ashok Rajc32b6b82005-10-30 14:59:54 -080012 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Viresh Kumardb701152012-10-23 01:29:03 +020018#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
Viresh Kumar5ff0a262013-08-06 22:53:03 +053020#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/cpufreq.h>
22#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/device.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053024#include <linux/init.h>
25#include <linux/kernel_stat.h>
26#include <linux/module.h>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080027#include <linux/mutex.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053028#include <linux/slab.h>
Viresh Kumar2f0aea92014-03-04 11:00:26 +080029#include <linux/suspend.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/* This one keeps track of the previously set governor of a removed CPU */
Dmitry Monakhove77b89f2009-10-05 00:38:55 +040046static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Viresh Kumar2f0aea92014-03-04 11:00:26 +080048/* Flag to suspend/resume CPUFreq governors */
49static bool cpufreq_suspended;
50
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +053051static inline bool has_target(void)
52{
53 return cpufreq_driver->target_index || cpufreq_driver->target;
54}
55
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080056/*
Viresh Kumar6eed9402013-08-06 22:53:11 +053057 * rwsem to guarantee that cpufreq driver module doesn't unload during critical
58 * sections
59 */
60static DECLARE_RWSEM(cpufreq_rwsem);
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/* internal prototypes */
Dave Jones29464f22009-01-18 01:37:11 -050063static int __cpufreq_governor(struct cpufreq_policy *policy,
64 unsigned int event);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080065static unsigned int __cpufreq_get(unsigned int cpu);
David Howells65f27f32006-11-22 14:55:48 +000066static void handle_update(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/**
Dave Jones32ee8c32006-02-28 00:43:23 -050069 * Two notifier lists: the "policy" list is involved in the
70 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 * "transition" list for kernel code that needs to handle
72 * changes to devices when the CPU clock speed changes.
73 * The mutex locks both lists.
74 */
Alan Sterne041c682006-03-27 01:16:30 -080075static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -070076static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -020078static bool init_cpufreq_transition_notifier_list_called;
Alan Sternb4dfdbb2006-10-04 02:17:06 -070079static int __init init_cpufreq_transition_notifier_list(void)
80{
81 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -020082 init_cpufreq_transition_notifier_list_called = true;
Alan Sternb4dfdbb2006-10-04 02:17:06 -070083 return 0;
84}
Linus Torvaldsb3438f82006-11-20 11:47:18 -080085pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -040087static int off __read_mostly;
Viresh Kumarda584452012-10-26 00:51:32 +020088static int cpufreq_disabled(void)
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -040089{
90 return off;
91}
92void disable_cpufreq(void)
93{
94 off = 1;
95}
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static LIST_HEAD(cpufreq_governor_list);
Dave Jones29464f22009-01-18 01:37:11 -050097static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000099bool have_governor_per_policy(void)
100{
Viresh Kumar0b981e72013-10-02 14:13:18 +0530101 return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000102}
Viresh Kumar3f869d62013-05-16 05:09:56 +0000103EXPORT_SYMBOL_GPL(have_governor_per_policy);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000104
Viresh Kumar944e9a02013-05-16 05:09:57 +0000105struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
106{
107 if (have_governor_per_policy())
108 return &policy->kobj;
109 else
110 return cpufreq_global_kobject;
111}
112EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
113
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000114static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
115{
116 u64 idle_time;
117 u64 cur_wall_time;
118 u64 busy_time;
119
120 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
121
122 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
123 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
124 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
125 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
126 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
127 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
128
129 idle_time = cur_wall_time - busy_time;
130 if (wall)
131 *wall = cputime_to_usecs(cur_wall_time);
132
133 return cputime_to_usecs(idle_time);
134}
135
136u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
137{
138 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
139
140 if (idle_time == -1ULL)
141 return get_cpu_idle_time_jiffy(cpu, wall);
142 else if (!io_busy)
143 idle_time += get_cpu_iowait_time_us(cpu, wall);
144
145 return idle_time;
146}
147EXPORT_SYMBOL_GPL(get_cpu_idle_time);
148
Viresh Kumar70e9e772013-10-03 20:29:07 +0530149/*
150 * This is a generic cpufreq init() routine which can be used by cpufreq
151 * drivers of SMP systems. It will do following:
152 * - validate & show freq table passed
153 * - set policies transition latency
154 * - policy->cpus with all possible CPUs
155 */
156int cpufreq_generic_init(struct cpufreq_policy *policy,
157 struct cpufreq_frequency_table *table,
158 unsigned int transition_latency)
159{
160 int ret;
161
162 ret = cpufreq_table_validate_and_show(policy, table);
163 if (ret) {
164 pr_err("%s: invalid frequency table: %d\n", __func__, ret);
165 return ret;
166 }
167
168 policy->cpuinfo.transition_latency = transition_latency;
169
170 /*
171 * The driver only supports the SMP configuartion where all processors
172 * share the clock and voltage and clock.
173 */
174 cpumask_setall(policy->cpus);
175
176 return 0;
177}
178EXPORT_SYMBOL_GPL(cpufreq_generic_init);
179
Viresh Kumar652ed952014-01-09 20:38:43 +0530180unsigned int cpufreq_generic_get(unsigned int cpu)
181{
182 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
183
184 if (!policy || IS_ERR(policy->clk)) {
185 pr_err("%s: No %s associated to cpu: %d\n", __func__,
186 policy ? "clk" : "policy", cpu);
187 return 0;
188 }
189
190 return clk_get_rate(policy->clk) / 1000;
191}
192EXPORT_SYMBOL_GPL(cpufreq_generic_get);
193
Viresh Kumar6eed9402013-08-06 22:53:11 +0530194struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530196 struct cpufreq_policy *policy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 unsigned long flags;
198
Viresh Kumar6eed9402013-08-06 22:53:11 +0530199 if (cpufreq_disabled() || (cpu >= nr_cpu_ids))
200 return NULL;
201
202 if (!down_read_trylock(&cpufreq_rwsem))
203 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 /* get the cpufreq driver */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000206 read_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Viresh Kumar6eed9402013-08-06 22:53:11 +0530208 if (cpufreq_driver) {
209 /* get the CPU */
210 policy = per_cpu(cpufreq_cpu_data, cpu);
211 if (policy)
212 kobject_get(&policy->kobj);
213 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200214
Viresh Kumar6eed9402013-08-06 22:53:11 +0530215 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530217 if (!policy)
Viresh Kumar6eed9402013-08-06 22:53:11 +0530218 up_read(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530220 return policy;
Stephen Boyda9144432012-07-20 18:14:38 +0000221}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
223
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530224void cpufreq_cpu_put(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000226 if (cpufreq_disabled())
227 return;
228
Viresh Kumar6eed9402013-08-06 22:53:11 +0530229 kobject_put(&policy->kobj);
230 up_read(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
236 *********************************************************************/
237
238/**
239 * adjust_jiffies - adjust the system "loops_per_jiffy"
240 *
241 * This function alters the system "loops_per_jiffy" for the clock
242 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500243 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 * per-CPU loops_per_jiffy value wherever possible.
245 */
246#ifndef CONFIG_SMP
247static unsigned long l_p_j_ref;
Viresh Kumarbb176f72013-06-19 14:19:33 +0530248static unsigned int l_p_j_ref_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Arjan van de Ven858119e2006-01-14 13:20:43 -0800250static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252 if (ci->flags & CPUFREQ_CONST_LOOPS)
253 return;
254
255 if (!l_p_j_ref_freq) {
256 l_p_j_ref = loops_per_jiffy;
257 l_p_j_ref_freq = ci->old;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200258 pr_debug("saving %lu as reference value for loops_per_jiffy; "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530259 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
Viresh Kumarbb176f72013-06-19 14:19:33 +0530261 if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700262 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530263 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
264 ci->new);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200265 pr_debug("scaling loops_per_jiffy to %lu "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530266 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
268}
269#else
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530270static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
271{
272 return;
273}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274#endif
275
Viresh Kumar0956df9c2013-06-19 14:19:34 +0530276static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530277 struct cpufreq_freqs *freqs, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 BUG_ON(irqs_disabled());
280
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000281 if (cpufreq_disabled())
282 return;
283
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200284 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200285 pr_debug("notification %u of frequency transition to %u kHz\n",
Dave Jonese4472cb2006-01-31 15:53:55 -0800286 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500291 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800292 * which is not equal to what the cpufreq core thinks is
293 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200295 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800296 if ((policy) && (policy->cpu == freqs->cpu) &&
297 (policy->cur) && (policy->cur != freqs->old)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200298 pr_debug("Warning: CPU frequency is"
Dave Jonese4472cb2006-01-31 15:53:55 -0800299 " %u, cpufreq assumed %u kHz.\n",
300 freqs->old, policy->cur);
301 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700304 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800305 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
307 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 case CPUFREQ_POSTCHANGE:
310 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200311 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
Thomas Renninger6f4f2722010-04-20 13:17:36 +0200312 (unsigned long)freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100313 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700314 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800315 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800316 if (likely(policy) && likely(policy->cpu == freqs->cpu))
317 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 break;
319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
Viresh Kumarbb176f72013-06-19 14:19:33 +0530321
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530322/**
323 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
324 * on frequency transition.
325 *
326 * This function calls the transition notifiers and the "adjust_jiffies"
327 * function. It is called twice on all CPU frequency changes that have
328 * external effects.
329 */
330void cpufreq_notify_transition(struct cpufreq_policy *policy,
331 struct cpufreq_freqs *freqs, unsigned int state)
332{
333 for_each_cpu(freqs->cpu, policy->cpus)
334 __cpufreq_notify_transition(policy, freqs, state);
335}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
337
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530338/* Do post notifications when there are chances that transition has failed */
339void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
340 struct cpufreq_freqs *freqs, int transition_failed)
341{
342 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
343 if (!transition_failed)
344 return;
345
346 swap(freqs->old, freqs->new);
347 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
348 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
349}
350EXPORT_SYMBOL_GPL(cpufreq_notify_post_transition);
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353/*********************************************************************
354 * SYSFS INTERFACE *
355 *********************************************************************/
Rashika Kheria8a5c74a2014-02-26 22:12:42 +0530356static ssize_t show_boost(struct kobject *kobj,
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100357 struct attribute *attr, char *buf)
358{
359 return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
360}
361
362static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
363 const char *buf, size_t count)
364{
365 int ret, enable;
366
367 ret = sscanf(buf, "%d", &enable);
368 if (ret != 1 || enable < 0 || enable > 1)
369 return -EINVAL;
370
371 if (cpufreq_boost_trigger_state(enable)) {
372 pr_err("%s: Cannot %s BOOST!\n", __func__,
373 enable ? "enable" : "disable");
374 return -EINVAL;
375 }
376
377 pr_debug("%s: cpufreq BOOST %s\n", __func__,
378 enable ? "enabled" : "disabled");
379
380 return count;
381}
382define_one_global_rw(boost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700384static struct cpufreq_governor *__find_governor(const char *str_governor)
385{
386 struct cpufreq_governor *t;
387
388 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
Dave Jones29464f22009-01-18 01:37:11 -0500389 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700390 return t;
391
392 return NULL;
393}
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395/**
396 * cpufreq_parse_governor - parse a governor string
397 */
Dave Jones905d77c2008-03-05 14:28:32 -0500398static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 struct cpufreq_governor **governor)
400{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700401 int err = -EINVAL;
402
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200403 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700404 goto out;
405
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200406 if (cpufreq_driver->setpolicy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
408 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700409 err = 0;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530410 } else if (!strnicmp(str_governor, "powersave",
411 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700413 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530415 } else if (has_target()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700417
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800418 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700419
420 t = __find_governor(str_governor);
421
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700422 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700423 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700424
Kees Cook1a8e1462011-05-04 08:38:56 -0700425 mutex_unlock(&cpufreq_governor_mutex);
426 ret = request_module("cpufreq_%s", str_governor);
427 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700428
Kees Cook1a8e1462011-05-04 08:38:56 -0700429 if (ret == 0)
430 t = __find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700431 }
432
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700433 if (t != NULL) {
434 *governor = t;
435 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700437
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800438 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
Dave Jones29464f22009-01-18 01:37:11 -0500440out:
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700441 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530445 * cpufreq_per_cpu_attr_read() / show_##file_name() -
446 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 *
448 * Write out information from cpufreq_driver->policy[cpu]; object must be
449 * "unsigned int".
450 */
451
Dave Jones32ee8c32006-02-28 00:43:23 -0500452#define show_one(file_name, object) \
453static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500454(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500455{ \
Dave Jones29464f22009-01-18 01:37:11 -0500456 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
459show_one(cpuinfo_min_freq, cpuinfo.min_freq);
460show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100461show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462show_one(scaling_min_freq, min);
463show_one(scaling_max_freq, max);
464show_one(scaling_cur_freq, cur);
465
Viresh Kumar037ce832013-10-02 14:13:16 +0530466static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530467 struct cpufreq_policy *new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469/**
470 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
471 */
472#define store_one(file_name, object) \
473static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500474(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{ \
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530476 int ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 struct cpufreq_policy new_policy; \
478 \
479 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
480 if (ret) \
481 return -EINVAL; \
482 \
Dave Jones29464f22009-01-18 01:37:11 -0500483 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (ret != 1) \
485 return -EINVAL; \
486 \
Viresh Kumar037ce832013-10-02 14:13:16 +0530487 ret = cpufreq_set_policy(policy, &new_policy); \
Thomas Renninger7970e082006-04-13 15:14:04 +0200488 policy->user_policy.object = policy->object; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 \
490 return ret ? ret : count; \
491}
492
Dave Jones29464f22009-01-18 01:37:11 -0500493store_one(scaling_min_freq, min);
494store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496/**
497 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
498 */
Dave Jones905d77c2008-03-05 14:28:32 -0500499static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
500 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800502 unsigned int cur_freq = __cpufreq_get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (!cur_freq)
504 return sprintf(buf, "<unknown>");
505 return sprintf(buf, "%u\n", cur_freq);
506}
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508/**
509 * show_scaling_governor - show the current policy for the specified CPU
510 */
Dave Jones905d77c2008-03-05 14:28:32 -0500511static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Dave Jones29464f22009-01-18 01:37:11 -0500513 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return sprintf(buf, "powersave\n");
515 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
516 return sprintf(buf, "performance\n");
517 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200518 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500519 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return -EINVAL;
521}
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523/**
524 * store_scaling_governor - store policy for the specified CPU
525 */
Dave Jones905d77c2008-03-05 14:28:32 -0500526static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
527 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530529 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 char str_governor[16];
531 struct cpufreq_policy new_policy;
532
533 ret = cpufreq_get_policy(&new_policy, policy->cpu);
534 if (ret)
535 return ret;
536
Dave Jones29464f22009-01-18 01:37:11 -0500537 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (ret != 1)
539 return -EINVAL;
540
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530541 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
542 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return -EINVAL;
544
Viresh Kumar037ce832013-10-02 14:13:16 +0530545 ret = cpufreq_set_policy(policy, &new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200546
547 policy->user_policy.policy = policy->policy;
548 policy->user_policy.governor = policy->governor;
Thomas Renninger7970e082006-04-13 15:14:04 +0200549
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530550 if (ret)
551 return ret;
552 else
553 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
556/**
557 * show_scaling_driver - show the cpufreq driver currently loaded
558 */
Dave Jones905d77c2008-03-05 14:28:32 -0500559static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200561 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
564/**
565 * show_scaling_available_governors - show the available CPUfreq governors
566 */
Dave Jones905d77c2008-03-05 14:28:32 -0500567static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
568 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 ssize_t i = 0;
571 struct cpufreq_governor *t;
572
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530573 if (!has_target()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 i += sprintf(buf, "performance powersave");
575 goto out;
576 }
577
578 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
Dave Jones29464f22009-01-18 01:37:11 -0500579 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
580 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200582 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500584out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 i += sprintf(&buf[i], "\n");
586 return i;
587}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700588
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800589ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
591 ssize_t i = 0;
592 unsigned int cpu;
593
Rusty Russell835481d2009-01-04 05:18:06 -0800594 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (i)
596 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
597 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
598 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500599 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
601 i += sprintf(&buf[i], "\n");
602 return i;
603}
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800604EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700606/**
607 * show_related_cpus - show the CPUs affected by each transition even if
608 * hw coordination is in use
609 */
610static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
611{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800612 return cpufreq_show_cpus(policy->related_cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700613}
614
615/**
616 * show_affected_cpus - show the CPUs affected by each transition
617 */
618static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
619{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800620 return cpufreq_show_cpus(policy->cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700621}
622
Venki Pallipadi9e769882007-10-26 10:18:21 -0700623static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500624 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700625{
626 unsigned int freq = 0;
627 unsigned int ret;
628
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700629 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700630 return -EINVAL;
631
632 ret = sscanf(buf, "%u", &freq);
633 if (ret != 1)
634 return -EINVAL;
635
636 policy->governor->store_setspeed(policy, freq);
637
638 return count;
639}
640
641static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
642{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700643 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700644 return sprintf(buf, "<unsupported>\n");
645
646 return policy->governor->show_setspeed(policy, buf);
647}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Thomas Renningere2f74f32009-11-19 12:31:01 +0100649/**
viresh kumar8bf1ac722012-10-23 01:23:33 +0200650 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100651 */
652static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
653{
654 unsigned int limit;
655 int ret;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200656 if (cpufreq_driver->bios_limit) {
657 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
Thomas Renningere2f74f32009-11-19 12:31:01 +0100658 if (!ret)
659 return sprintf(buf, "%u\n", limit);
660 }
661 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
662}
663
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200664cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
665cpufreq_freq_attr_ro(cpuinfo_min_freq);
666cpufreq_freq_attr_ro(cpuinfo_max_freq);
667cpufreq_freq_attr_ro(cpuinfo_transition_latency);
668cpufreq_freq_attr_ro(scaling_available_governors);
669cpufreq_freq_attr_ro(scaling_driver);
670cpufreq_freq_attr_ro(scaling_cur_freq);
671cpufreq_freq_attr_ro(bios_limit);
672cpufreq_freq_attr_ro(related_cpus);
673cpufreq_freq_attr_ro(affected_cpus);
674cpufreq_freq_attr_rw(scaling_min_freq);
675cpufreq_freq_attr_rw(scaling_max_freq);
676cpufreq_freq_attr_rw(scaling_governor);
677cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Dave Jones905d77c2008-03-05 14:28:32 -0500679static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 &cpuinfo_min_freq.attr,
681 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100682 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 &scaling_min_freq.attr,
684 &scaling_max_freq.attr,
685 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700686 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 &scaling_governor.attr,
688 &scaling_driver.attr,
689 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700690 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 NULL
692};
693
Dave Jones29464f22009-01-18 01:37:11 -0500694#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
695#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Dave Jones29464f22009-01-18 01:37:11 -0500697static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Dave Jones905d77c2008-03-05 14:28:32 -0500699 struct cpufreq_policy *policy = to_policy(kobj);
700 struct freq_attr *fattr = to_attr(attr);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530701 ssize_t ret;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530702
703 if (!down_read_trylock(&cpufreq_rwsem))
Viresh Kumar1b750e32013-10-02 14:13:09 +0530704 return -EINVAL;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800705
viresh kumarad7722d2013-10-18 19:10:15 +0530706 down_read(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800707
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530708 if (fattr->show)
709 ret = fattr->show(policy, buf);
710 else
711 ret = -EIO;
712
viresh kumarad7722d2013-10-18 19:10:15 +0530713 up_read(&policy->rwsem);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530714 up_read(&cpufreq_rwsem);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return ret;
717}
718
Dave Jones905d77c2008-03-05 14:28:32 -0500719static ssize_t store(struct kobject *kobj, struct attribute *attr,
720 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Dave Jones905d77c2008-03-05 14:28:32 -0500722 struct cpufreq_policy *policy = to_policy(kobj);
723 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500724 ssize_t ret = -EINVAL;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530725
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530726 get_online_cpus();
727
728 if (!cpu_online(policy->cpu))
729 goto unlock;
730
Viresh Kumar6eed9402013-08-06 22:53:11 +0530731 if (!down_read_trylock(&cpufreq_rwsem))
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530732 goto unlock;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800733
viresh kumarad7722d2013-10-18 19:10:15 +0530734 down_write(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800735
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530736 if (fattr->store)
737 ret = fattr->store(policy, buf, count);
738 else
739 ret = -EIO;
740
viresh kumarad7722d2013-10-18 19:10:15 +0530741 up_write(&policy->rwsem);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530742
Viresh Kumar6eed9402013-08-06 22:53:11 +0530743 up_read(&cpufreq_rwsem);
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530744unlock:
745 put_online_cpus();
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return ret;
748}
749
Dave Jones905d77c2008-03-05 14:28:32 -0500750static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
Dave Jones905d77c2008-03-05 14:28:32 -0500752 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200753 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 complete(&policy->kobj_unregister);
755}
756
Emese Revfy52cf25d2010-01-19 02:58:23 +0100757static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 .show = show,
759 .store = store,
760};
761
762static struct kobj_type ktype_cpufreq = {
763 .sysfs_ops = &sysfs_ops,
764 .default_attrs = default_attrs,
765 .release = cpufreq_sysfs_release,
766};
767
Viresh Kumar2361be22013-05-17 16:09:09 +0530768struct kobject *cpufreq_global_kobject;
769EXPORT_SYMBOL(cpufreq_global_kobject);
770
771static int cpufreq_global_kobject_usage;
772
773int cpufreq_get_global_kobject(void)
774{
775 if (!cpufreq_global_kobject_usage++)
776 return kobject_add(cpufreq_global_kobject,
777 &cpu_subsys.dev_root->kobj, "%s", "cpufreq");
778
779 return 0;
780}
781EXPORT_SYMBOL(cpufreq_get_global_kobject);
782
783void cpufreq_put_global_kobject(void)
784{
785 if (!--cpufreq_global_kobject_usage)
786 kobject_del(cpufreq_global_kobject);
787}
788EXPORT_SYMBOL(cpufreq_put_global_kobject);
789
790int cpufreq_sysfs_create_file(const struct attribute *attr)
791{
792 int ret = cpufreq_get_global_kobject();
793
794 if (!ret) {
795 ret = sysfs_create_file(cpufreq_global_kobject, attr);
796 if (ret)
797 cpufreq_put_global_kobject();
798 }
799
800 return ret;
801}
802EXPORT_SYMBOL(cpufreq_sysfs_create_file);
803
804void cpufreq_sysfs_remove_file(const struct attribute *attr)
805{
806 sysfs_remove_file(cpufreq_global_kobject, attr);
807 cpufreq_put_global_kobject();
808}
809EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
810
Dave Jones19d6f7e2009-07-08 17:35:39 -0400811/* symlink affected CPUs */
Viresh Kumar308b60e2013-07-31 14:35:14 +0200812static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400813{
814 unsigned int j;
815 int ret = 0;
816
817 for_each_cpu(j, policy->cpus) {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800818 struct device *cpu_dev;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400819
Viresh Kumar308b60e2013-07-31 14:35:14 +0200820 if (j == policy->cpu)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400821 continue;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400822
Viresh Kumare8fdde12013-07-31 14:31:33 +0200823 pr_debug("Adding link for CPU: %u\n", j);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800824 cpu_dev = get_cpu_device(j);
825 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Dave Jones19d6f7e2009-07-08 17:35:39 -0400826 "cpufreq");
Rafael J. Wysocki71c34612013-08-04 01:19:34 +0200827 if (ret)
828 break;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400829 }
830 return ret;
831}
832
Viresh Kumar308b60e2013-07-31 14:35:14 +0200833static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800834 struct device *dev)
Dave Jones909a6942009-07-08 18:05:42 -0400835{
836 struct freq_attr **drv_attr;
Dave Jones909a6942009-07-08 18:05:42 -0400837 int ret = 0;
Dave Jones909a6942009-07-08 18:05:42 -0400838
839 /* prepare interface data */
840 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800841 &dev->kobj, "cpufreq");
Dave Jones909a6942009-07-08 18:05:42 -0400842 if (ret)
843 return ret;
844
845 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200846 drv_attr = cpufreq_driver->attr;
Dave Jones909a6942009-07-08 18:05:42 -0400847 while ((drv_attr) && (*drv_attr)) {
848 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
849 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200850 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400851 drv_attr++;
852 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200853 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -0400854 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
855 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200856 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400857 }
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530858 if (has_target()) {
Dave Jones909a6942009-07-08 18:05:42 -0400859 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
860 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200861 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400862 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200863 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +0100864 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
865 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200866 goto err_out_kobj_put;
Thomas Renningere2f74f32009-11-19 12:31:01 +0100867 }
Dave Jones909a6942009-07-08 18:05:42 -0400868
Viresh Kumar308b60e2013-07-31 14:35:14 +0200869 ret = cpufreq_add_dev_symlink(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400870 if (ret)
871 goto err_out_kobj_put;
872
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530873 return ret;
874
875err_out_kobj_put:
876 kobject_put(&policy->kobj);
877 wait_for_completion(&policy->kobj_unregister);
878 return ret;
879}
880
881static void cpufreq_init_policy(struct cpufreq_policy *policy)
882{
viresh kumar6e2c89d2014-03-04 11:43:59 +0800883 struct cpufreq_governor *gov = NULL;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530884 struct cpufreq_policy new_policy;
885 int ret = 0;
886
Viresh Kumard5b73cd2013-08-06 22:53:06 +0530887 memcpy(&new_policy, policy, sizeof(*policy));
Jason Barona27a9ab2013-12-19 22:50:50 +0000888
viresh kumar6e2c89d2014-03-04 11:43:59 +0800889 /* Update governor of new_policy to the governor used before hotplug */
890 gov = __find_governor(per_cpu(cpufreq_cpu_governor, policy->cpu));
891 if (gov)
892 pr_debug("Restoring governor %s for cpu %d\n",
893 policy->governor->name, policy->cpu);
894 else
895 gov = CPUFREQ_DEFAULT_GOVERNOR;
896
897 new_policy.governor = gov;
898
Jason Barona27a9ab2013-12-19 22:50:50 +0000899 /* Use the default policy if its valid. */
900 if (cpufreq_driver->setpolicy)
viresh kumar6e2c89d2014-03-04 11:43:59 +0800901 cpufreq_parse_governor(gov->name, &new_policy.policy, NULL);
Dave Jonesecf7e462009-07-08 18:48:47 -0400902
903 /* set default policy */
Viresh Kumar037ce832013-10-02 14:13:16 +0530904 ret = cpufreq_set_policy(policy, &new_policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400905 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200906 pr_debug("setting policy failed\n");
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200907 if (cpufreq_driver->exit)
908 cpufreq_driver->exit(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400909 }
Dave Jones909a6942009-07-08 18:05:42 -0400910}
911
Viresh Kumarfcf80582013-01-29 14:39:08 +0000912#ifdef CONFIG_HOTPLUG_CPU
Viresh Kumard8d3b472013-08-04 01:20:07 +0200913static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,
Viresh Kumar42f921a2013-12-20 21:26:02 +0530914 unsigned int cpu, struct device *dev)
Viresh Kumarfcf80582013-01-29 14:39:08 +0000915{
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530916 int ret = 0;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000917 unsigned long flags;
918
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530919 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +0530920 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
921 if (ret) {
922 pr_err("%s: Failed to stop governor\n", __func__);
923 return ret;
924 }
925 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000926
viresh kumarad7722d2013-10-18 19:10:15 +0530927 down_write(&policy->rwsem);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530928
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000929 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530930
Viresh Kumarfcf80582013-01-29 14:39:08 +0000931 cpumask_set_cpu(cpu, policy->cpus);
932 per_cpu(cpufreq_cpu_data, cpu) = policy;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000933 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000934
viresh kumarad7722d2013-10-18 19:10:15 +0530935 up_write(&policy->rwsem);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530936
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530937 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +0530938 if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
939 (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
940 pr_err("%s: Failed to start governor\n", __func__);
941 return ret;
942 }
Viresh Kumar820c6ca2013-04-22 00:48:03 +0200943 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000944
Viresh Kumar42f921a2013-12-20 21:26:02 +0530945 return sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
Viresh Kumarfcf80582013-01-29 14:39:08 +0000946}
947#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530949static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu)
950{
951 struct cpufreq_policy *policy;
952 unsigned long flags;
953
Lan Tianyu44871c92013-09-11 15:05:05 +0800954 read_lock_irqsave(&cpufreq_driver_lock, flags);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530955
956 policy = per_cpu(cpufreq_cpu_data_fallback, cpu);
957
Lan Tianyu44871c92013-09-11 15:05:05 +0800958 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530959
viresh kumar6e2c89d2014-03-04 11:43:59 +0800960 policy->governor = NULL;
961
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530962 return policy;
963}
964
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +0530965static struct cpufreq_policy *cpufreq_policy_alloc(void)
966{
967 struct cpufreq_policy *policy;
968
969 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
970 if (!policy)
971 return NULL;
972
973 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
974 goto err_free_policy;
975
976 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
977 goto err_free_cpumask;
978
Lukasz Majewskic88a1f82013-08-06 22:53:08 +0530979 INIT_LIST_HEAD(&policy->policy_list);
viresh kumarad7722d2013-10-18 19:10:15 +0530980 init_rwsem(&policy->rwsem);
981
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +0530982 return policy;
983
984err_free_cpumask:
985 free_cpumask_var(policy->cpus);
986err_free_policy:
987 kfree(policy);
988
989 return NULL;
990}
991
Viresh Kumar42f921a2013-12-20 21:26:02 +0530992static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
993{
994 struct kobject *kobj;
995 struct completion *cmp;
996
Viresh Kumarfcd7af92014-01-07 07:10:10 +0530997 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
998 CPUFREQ_REMOVE_POLICY, policy);
999
Viresh Kumar42f921a2013-12-20 21:26:02 +05301000 down_read(&policy->rwsem);
1001 kobj = &policy->kobj;
1002 cmp = &policy->kobj_unregister;
1003 up_read(&policy->rwsem);
1004 kobject_put(kobj);
1005
1006 /*
1007 * We need to make sure that the underlying kobj is
1008 * actually not referenced anymore by anybody before we
1009 * proceed with unloading.
1010 */
1011 pr_debug("waiting for dropping of refcount\n");
1012 wait_for_completion(cmp);
1013 pr_debug("wait complete\n");
1014}
1015
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301016static void cpufreq_policy_free(struct cpufreq_policy *policy)
1017{
1018 free_cpumask_var(policy->related_cpus);
1019 free_cpumask_var(policy->cpus);
1020 kfree(policy);
1021}
1022
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301023static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
1024{
Srivatsa S. Bhat99ec8992013-09-12 17:29:09 +05301025 if (WARN_ON(cpu == policy->cpu))
Srivatsa S. Bhatcb38ed52013-09-12 01:43:42 +05301026 return;
1027
viresh kumarad7722d2013-10-18 19:10:15 +05301028 down_write(&policy->rwsem);
Viresh Kumar8efd5762013-09-17 10:22:11 +05301029
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301030 policy->last_cpu = policy->cpu;
1031 policy->cpu = cpu;
1032
viresh kumarad7722d2013-10-18 19:10:15 +05301033 up_write(&policy->rwsem);
Viresh Kumar8efd5762013-09-17 10:22:11 +05301034
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301035 cpufreq_frequency_table_update_policy_cpu(policy);
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301036 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1037 CPUFREQ_UPDATE_POLICY_CPU, policy);
1038}
1039
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301040static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif,
1041 bool frozen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
Viresh Kumarfcf80582013-01-29 14:39:08 +00001043 unsigned int j, cpu = dev->id;
Viresh Kumar65922462013-02-07 10:56:03 +05301044 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 unsigned long flags;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001047#ifdef CONFIG_HOTPLUG_CPU
Viresh Kumar1b274292013-08-20 12:08:26 +05301048 struct cpufreq_policy *tpolicy;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001049#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Ashok Rajc32b6b82005-10-30 14:59:54 -08001051 if (cpu_is_offline(cpu))
1052 return 0;
1053
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001054 pr_debug("adding CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056#ifdef CONFIG_SMP
1057 /* check whether a different CPU already registered this
1058 * CPU because it is in the same boat. */
1059 policy = cpufreq_cpu_get(cpu);
1060 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -05001061 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return 0;
1063 }
Li Zhong5025d622013-08-21 01:31:08 +02001064#endif
Viresh Kumarfcf80582013-01-29 14:39:08 +00001065
Viresh Kumar6eed9402013-08-06 22:53:11 +05301066 if (!down_read_trylock(&cpufreq_rwsem))
1067 return 0;
1068
Viresh Kumarfcf80582013-01-29 14:39:08 +00001069#ifdef CONFIG_HOTPLUG_CPU
1070 /* Check if this cpu was hot-unplugged earlier and has siblings */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001071 read_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar1b274292013-08-20 12:08:26 +05301072 list_for_each_entry(tpolicy, &cpufreq_policy_list, policy_list) {
1073 if (cpumask_test_cpu(cpu, tpolicy->related_cpus)) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001074 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301075 ret = cpufreq_add_policy_cpu(tpolicy, cpu, dev);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301076 up_read(&cpufreq_rwsem);
1077 return ret;
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301078 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001079 }
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001080 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +00001081#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001083 /*
1084 * Restore the saved policy when doing light-weight init and fall back
1085 * to the full init if that fails.
1086 */
1087 policy = frozen ? cpufreq_policy_restore(cpu) : NULL;
1088 if (!policy) {
1089 frozen = false;
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301090 policy = cpufreq_policy_alloc();
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001091 if (!policy)
1092 goto nomem_out;
1093 }
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301094
1095 /*
1096 * In the resume path, since we restore a saved policy, the assignment
1097 * to policy->cpu is like an update of the existing policy, rather than
1098 * the creation of a brand new one. So we need to perform this update
1099 * by invoking update_policy_cpu().
1100 */
1101 if (frozen && cpu != policy->cpu)
1102 update_policy_cpu(policy, cpu);
1103 else
1104 policy->cpu = cpu;
1105
Rusty Russell835481d2009-01-04 05:18:06 -08001106 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 init_completion(&policy->kobj_unregister);
David Howells65f27f32006-11-22 14:55:48 +00001109 INIT_WORK(&policy->update, handle_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 /* call driver. From then on the cpufreq must be able
1112 * to accept all calls to ->verify and ->setpolicy for this CPU
1113 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001114 ret = cpufreq_driver->init(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001116 pr_debug("initialization failed\n");
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301117 goto err_set_policy_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001119
Viresh Kumar5a7e56a2014-03-04 11:44:00 +08001120 /* related cpus should atleast have policy->cpus */
1121 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
1122
1123 /*
1124 * affected cpus must always be the one, which are online. We aren't
1125 * managing offline cpus here.
1126 */
1127 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1128
1129 if (!frozen) {
1130 policy->user_policy.min = policy->min;
1131 policy->user_policy.max = policy->max;
1132 }
1133
Viresh Kumar4e97b632014-03-04 11:44:01 +08001134 down_write(&policy->rwsem);
Viresh Kumar652ed952014-01-09 20:38:43 +05301135 write_lock_irqsave(&cpufreq_driver_lock, flags);
1136 for_each_cpu(j, policy->cpus)
1137 per_cpu(cpufreq_cpu_data, j) = policy;
1138 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1139
Viresh Kumarda60ce92013-10-03 20:28:30 +05301140 if (cpufreq_driver->get) {
1141 policy->cur = cpufreq_driver->get(policy->cpu);
1142 if (!policy->cur) {
1143 pr_err("%s: ->get() failed\n", __func__);
1144 goto err_get_freq;
1145 }
1146 }
1147
Viresh Kumard3916692013-12-03 11:20:46 +05301148 /*
1149 * Sometimes boot loaders set CPU frequency to a value outside of
1150 * frequency table present with cpufreq core. In such cases CPU might be
1151 * unstable if it has to run on that frequency for long duration of time
1152 * and so its better to set it to a frequency which is specified in
1153 * freq-table. This also makes cpufreq stats inconsistent as
1154 * cpufreq-stats would fail to register because current frequency of CPU
1155 * isn't found in freq-table.
1156 *
1157 * Because we don't want this change to effect boot process badly, we go
1158 * for the next freq which is >= policy->cur ('cur' must be set by now,
1159 * otherwise we will end up setting freq to lowest of the table as 'cur'
1160 * is initialized to zero).
1161 *
1162 * We are passing target-freq as "policy->cur - 1" otherwise
1163 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1164 * equal to target-freq.
1165 */
1166 if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1167 && has_target()) {
1168 /* Are we running at unknown frequency ? */
1169 ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1170 if (ret == -EINVAL) {
1171 /* Warn user and fix it */
1172 pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1173 __func__, policy->cpu, policy->cur);
1174 ret = __cpufreq_driver_target(policy, policy->cur - 1,
1175 CPUFREQ_RELATION_L);
1176
1177 /*
1178 * Reaching here after boot in a few seconds may not
1179 * mean that system will remain stable at "unknown"
1180 * frequency for longer duration. Hence, a BUG_ON().
1181 */
1182 BUG_ON(ret);
1183 pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1184 __func__, policy->cpu, policy->cur);
1185 }
1186 }
1187
Thomas Renningera1531ac2008-07-29 22:32:58 -07001188 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1189 CPUFREQ_START, policy);
1190
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301191 if (!frozen) {
Viresh Kumar308b60e2013-07-31 14:35:14 +02001192 ret = cpufreq_add_dev_interface(policy, dev);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301193 if (ret)
1194 goto err_out_unregister;
Viresh Kumarfcd7af92014-01-07 07:10:10 +05301195 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1196 CPUFREQ_CREATE_POLICY, policy);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301197 }
Dave Jones8ff69732006-03-05 03:37:23 -05001198
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301199 write_lock_irqsave(&cpufreq_driver_lock, flags);
1200 list_add(&policy->policy_list, &cpufreq_policy_list);
1201 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1202
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301203 cpufreq_init_policy(policy);
1204
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301205 if (!frozen) {
1206 policy->user_policy.policy = policy->policy;
1207 policy->user_policy.governor = policy->governor;
1208 }
Viresh Kumar4e97b632014-03-04 11:44:01 +08001209 up_write(&policy->rwsem);
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301210
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001211 kobject_uevent(&policy->kobj, KOBJ_ADD);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301212 up_read(&cpufreq_rwsem);
1213
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001214 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 return 0;
1217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218err_out_unregister:
Viresh Kumar652ed952014-01-09 20:38:43 +05301219err_get_freq:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001220 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar474deff2013-08-20 12:08:25 +05301221 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -07001222 per_cpu(cpufreq_cpu_data, j) = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001223 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Viresh Kumarda60ce92013-10-03 20:28:30 +05301225 if (cpufreq_driver->exit)
1226 cpufreq_driver->exit(policy);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301227err_set_policy_cpu:
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001228 if (frozen) {
1229 /* Do not leave stale fallback data behind. */
1230 per_cpu(cpufreq_cpu_data_fallback, cpu) = NULL;
Viresh Kumar42f921a2013-12-20 21:26:02 +05301231 cpufreq_policy_put_kobj(policy);
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001232 }
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301233 cpufreq_policy_free(policy);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235nomem_out:
Viresh Kumar6eed9402013-08-06 22:53:11 +05301236 up_read(&cpufreq_rwsem);
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return ret;
1239}
1240
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301241/**
1242 * cpufreq_add_dev - add a CPU device
1243 *
1244 * Adds the cpufreq interface for a CPU device.
1245 *
1246 * The Oracle says: try running cpufreq registration/unregistration concurrently
1247 * with with cpu hotplugging and all hell will break loose. Tried to clean this
1248 * mess up, but more thorough testing is needed. - Mathieu
1249 */
1250static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1251{
1252 return __cpufreq_add_dev(dev, sif, false);
1253}
1254
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301255static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy *policy,
Viresh Kumar42f921a2013-12-20 21:26:02 +05301256 unsigned int old_cpu)
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301257{
1258 struct device *cpu_dev;
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301259 int ret;
1260
1261 /* first sibling now owns the new sysfs dir */
Viresh Kumar9c8f1ee2013-09-12 17:06:33 +05301262 cpu_dev = get_cpu_device(cpumask_any_but(policy->cpus, old_cpu));
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301263
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301264 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301265 ret = kobject_move(&policy->kobj, &cpu_dev->kobj);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301266 if (ret) {
1267 pr_err("%s: Failed to move kobj: %d", __func__, ret);
1268
viresh kumarad7722d2013-10-18 19:10:15 +05301269 down_write(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301270 cpumask_set_cpu(old_cpu, policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301271 up_write(&policy->rwsem);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301272
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301273 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301274 "cpufreq");
1275
1276 return -EINVAL;
1277 }
1278
1279 return cpu_dev->id;
1280}
1281
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301282static int __cpufreq_remove_dev_prepare(struct device *dev,
1283 struct subsys_interface *sif,
1284 bool frozen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301286 unsigned int cpu = dev->id, cpus;
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301287 int new_cpu, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 unsigned long flags;
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301289 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001291 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001293 write_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301295 policy = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301297 /* Save the policy somewhere when doing a light-weight tear-down */
1298 if (frozen)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301299 per_cpu(cpufreq_cpu_data_fallback, cpu) = policy;
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301300
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001301 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301303 if (!policy) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001304 pr_debug("%s: No cpu_data found\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301308 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301309 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1310 if (ret) {
1311 pr_err("%s: Failed to stop governor\n", __func__);
1312 return ret;
1313 }
1314 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001315
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001316 if (!cpufreq_driver->setpolicy)
Dirk Brandewiefa69e332013-02-06 09:02:11 -08001317 strncpy(per_cpu(cpufreq_cpu_governor, cpu),
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301318 policy->governor->name, CPUFREQ_NAME_LEN);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001319
viresh kumarad7722d2013-10-18 19:10:15 +05301320 down_read(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301321 cpus = cpumask_weight(policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301322 up_read(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Srivatsa S. Bhat61173f22013-09-12 01:43:25 +05301324 if (cpu != policy->cpu) {
viresh kumar6964d912014-02-17 14:52:11 +05301325 sysfs_remove_link(&dev->kobj, "cpufreq");
Viresh Kumar73bf0fc2013-02-05 22:21:14 +01001326 } else if (cpus > 1) {
Viresh Kumar42f921a2013-12-20 21:26:02 +05301327 new_cpu = cpufreq_nominate_new_policy_cpu(policy, cpu);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301328 if (new_cpu >= 0) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301329 update_policy_cpu(policy, new_cpu);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301330
1331 if (!frozen) {
Viresh Kumar75949c92013-10-02 14:13:13 +05301332 pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
1333 __func__, new_cpu, cpu);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301334 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001335 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001336 }
Venki Pallipadiec282972007-03-26 12:03:19 -07001337
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301338 return 0;
1339}
1340
1341static int __cpufreq_remove_dev_finish(struct device *dev,
1342 struct subsys_interface *sif,
1343 bool frozen)
1344{
1345 unsigned int cpu = dev->id, cpus;
1346 int ret;
1347 unsigned long flags;
1348 struct cpufreq_policy *policy;
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301349
1350 read_lock_irqsave(&cpufreq_driver_lock, flags);
1351 policy = per_cpu(cpufreq_cpu_data, cpu);
1352 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1353
1354 if (!policy) {
1355 pr_debug("%s: No cpu_data found\n", __func__);
1356 return -EINVAL;
1357 }
1358
viresh kumarad7722d2013-10-18 19:10:15 +05301359 down_write(&policy->rwsem);
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301360 cpus = cpumask_weight(policy->cpus);
Viresh Kumar9c8f1ee2013-09-12 17:06:33 +05301361
1362 if (cpus > 1)
1363 cpumask_clear_cpu(cpu, policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301364 up_write(&policy->rwsem);
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301365
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001366 /* If cpu is last user of policy, free policy */
1367 if (cpus == 1) {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301368 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301369 ret = __cpufreq_governor(policy,
1370 CPUFREQ_GOV_POLICY_EXIT);
1371 if (ret) {
1372 pr_err("%s: Failed to exit governor\n",
1373 __func__);
1374 return ret;
1375 }
Viresh Kumaredab2fb2013-08-20 12:08:22 +05301376 }
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001377
Viresh Kumar42f921a2013-12-20 21:26:02 +05301378 if (!frozen)
1379 cpufreq_policy_put_kobj(policy);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301380
1381 /*
1382 * Perform the ->exit() even during light-weight tear-down,
1383 * since this is a core component, and is essential for the
1384 * subsequent light-weight ->init() to succeed.
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001385 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001386 if (cpufreq_driver->exit)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301387 cpufreq_driver->exit(policy);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001388
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301389 /* Remove policy from list of active policies */
1390 write_lock_irqsave(&cpufreq_driver_lock, flags);
1391 list_del(&policy->policy_list);
1392 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1393
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301394 if (!frozen)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301395 cpufreq_policy_free(policy);
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001396 } else {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301397 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301398 if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
1399 (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
1400 pr_err("%s: Failed to start governor\n",
1401 __func__);
1402 return ret;
1403 }
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001404 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Viresh Kumar474deff2013-08-20 12:08:25 +05301407 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return 0;
1409}
1410
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301411/**
Viresh Kumar27a862e2013-10-02 14:13:14 +05301412 * cpufreq_remove_dev - remove a CPU device
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301413 *
1414 * Removes the cpufreq interface for a CPU device.
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301415 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001416static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001417{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001418 unsigned int cpu = dev->id;
Viresh Kumar27a862e2013-10-02 14:13:14 +05301419 int ret;
Venki Pallipadiec282972007-03-26 12:03:19 -07001420
1421 if (cpu_is_offline(cpu))
1422 return 0;
1423
Viresh Kumar27a862e2013-10-02 14:13:14 +05301424 ret = __cpufreq_remove_dev_prepare(dev, sif, false);
1425
1426 if (!ret)
1427 ret = __cpufreq_remove_dev_finish(dev, sif, false);
1428
1429 return ret;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001430}
1431
David Howells65f27f32006-11-22 14:55:48 +00001432static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
David Howells65f27f32006-11-22 14:55:48 +00001434 struct cpufreq_policy *policy =
1435 container_of(work, struct cpufreq_policy, update);
1436 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001437 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 cpufreq_update_policy(cpu);
1439}
1440
1441/**
Viresh Kumarbb176f72013-06-19 14:19:33 +05301442 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1443 * in deep trouble.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 * @cpu: cpu number
1445 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1446 * @new_freq: CPU frequency the CPU actually runs at
1447 *
Dave Jones29464f22009-01-18 01:37:11 -05001448 * We adjust to current frequency first, and need to clean up later.
1449 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301451static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1452 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301454 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301456 unsigned long flags;
1457
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001458 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 freqs.old = old_freq;
1462 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301463
1464 read_lock_irqsave(&cpufreq_driver_lock, flags);
1465 policy = per_cpu(cpufreq_cpu_data, cpu);
1466 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1467
1468 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1469 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470}
1471
Dave Jones32ee8c32006-02-28 00:43:23 -05001472/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301473 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001474 * @cpu: CPU number
1475 *
1476 * This is the last known freq, without actually getting it from the driver.
1477 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1478 */
1479unsigned int cpufreq_quick_get(unsigned int cpu)
1480{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001481 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301482 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001483
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001484 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1485 return cpufreq_driver->get(cpu);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001486
1487 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001488 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301489 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001490 cpufreq_cpu_put(policy);
1491 }
1492
Dave Jones4d34a672008-02-07 16:33:49 -05001493 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001494}
1495EXPORT_SYMBOL(cpufreq_quick_get);
1496
Jesse Barnes3d737102011-06-28 10:59:12 -07001497/**
1498 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1499 * @cpu: CPU number
1500 *
1501 * Just return the max possible frequency for a given CPU.
1502 */
1503unsigned int cpufreq_quick_get_max(unsigned int cpu)
1504{
1505 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1506 unsigned int ret_freq = 0;
1507
1508 if (policy) {
1509 ret_freq = policy->max;
1510 cpufreq_cpu_put(policy);
1511 }
1512
1513 return ret_freq;
1514}
1515EXPORT_SYMBOL(cpufreq_quick_get_max);
1516
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001517static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001519 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301520 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001522 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001523 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001525 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301527 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001528 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301529 /* verify no discrepancy between actual and
1530 saved value exists */
1531 if (unlikely(ret_freq != policy->cur)) {
1532 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 schedule_work(&policy->update);
1534 }
1535 }
1536
Dave Jones4d34a672008-02-07 16:33:49 -05001537 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001538}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001540/**
1541 * cpufreq_get - get the current CPU frequency (in kHz)
1542 * @cpu: CPU number
1543 *
1544 * Get the CPU current (static) CPU frequency
1545 */
1546unsigned int cpufreq_get(unsigned int cpu)
1547{
Aaron Plattner999976e2014-03-04 12:42:15 -08001548 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001549 unsigned int ret_freq = 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001550
Aaron Plattner999976e2014-03-04 12:42:15 -08001551 if (policy) {
1552 down_read(&policy->rwsem);
1553 ret_freq = __cpufreq_get(cpu);
1554 up_read(&policy->rwsem);
Viresh Kumar26ca8692013-09-20 22:37:31 +05301555
Aaron Plattner999976e2014-03-04 12:42:15 -08001556 cpufreq_cpu_put(policy);
1557 }
Viresh Kumar6eed9402013-08-06 22:53:11 +05301558
Dave Jones4d34a672008-02-07 16:33:49 -05001559 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
1561EXPORT_SYMBOL(cpufreq_get);
1562
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001563static struct subsys_interface cpufreq_interface = {
1564 .name = "cpufreq",
1565 .subsys = &cpu_subsys,
1566 .add_dev = cpufreq_add_dev,
1567 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001568};
1569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001571 * cpufreq_suspend() - Suspend CPUFreq governors
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001572 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001573 * Called during system wide Suspend/Hibernate cycles for suspending governors
1574 * as some platforms can't change frequency after this point in suspend cycle.
1575 * Because some of the devices (like: i2c, regulators, etc) they use for
1576 * changing frequency are suspended quickly after this point.
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001577 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001578void cpufreq_suspend(void)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001579{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301580 struct cpufreq_policy *policy;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001581
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001582 if (!cpufreq_driver)
1583 return;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001584
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001585 if (!has_target())
1586 return;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001587
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001588 pr_debug("%s: Suspending Governors\n", __func__);
1589
1590 list_for_each_entry(policy, &cpufreq_policy_list, policy_list) {
1591 if (__cpufreq_governor(policy, CPUFREQ_GOV_STOP))
1592 pr_err("%s: Failed to stop governor for policy: %p\n",
1593 __func__, policy);
1594 else if (cpufreq_driver->suspend
1595 && cpufreq_driver->suspend(policy))
1596 pr_err("%s: Failed to suspend driver: %p\n", __func__,
1597 policy);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001598 }
1599
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001600 cpufreq_suspended = true;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001601}
1602
1603/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001604 * cpufreq_resume() - Resume CPUFreq governors
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001606 * Called during system wide Suspend/Hibernate cycle for resuming governors that
1607 * are suspended with cpufreq_suspend().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001609void cpufreq_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301611 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001613 if (!cpufreq_driver)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001614 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001616 if (!has_target())
1617 return;
1618
1619 pr_debug("%s: Resuming Governors\n", __func__);
1620
1621 cpufreq_suspended = false;
1622
1623 list_for_each_entry(policy, &cpufreq_policy_list, policy_list) {
1624 if (__cpufreq_governor(policy, CPUFREQ_GOV_START)
1625 || __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))
1626 pr_err("%s: Failed to start governor for policy: %p\n",
1627 __func__, policy);
1628 else if (cpufreq_driver->resume
1629 && cpufreq_driver->resume(policy))
1630 pr_err("%s: Failed to resume driver: %p\n", __func__,
1631 policy);
1632
1633 /*
1634 * schedule call cpufreq_update_policy() for boot CPU, i.e. last
1635 * policy in list. It will verify that the current freq is in
1636 * sync with what we believe it to be.
1637 */
1638 if (list_is_last(&policy->policy_list, &cpufreq_policy_list))
1639 schedule_work(&policy->update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641}
1642
Borislav Petkov9d950462013-01-20 10:24:28 +00001643/**
1644 * cpufreq_get_current_driver - return current driver's name
1645 *
1646 * Return the name string of the currently loaded cpufreq driver
1647 * or NULL, if none.
1648 */
1649const char *cpufreq_get_current_driver(void)
1650{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001651 if (cpufreq_driver)
1652 return cpufreq_driver->name;
1653
1654 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001655}
1656EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
1658/*********************************************************************
1659 * NOTIFIER LISTS INTERFACE *
1660 *********************************************************************/
1661
1662/**
1663 * cpufreq_register_notifier - register a driver with cpufreq
1664 * @nb: notifier function to register
1665 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1666 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001667 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 * are notified about clock rate changes (once before and once after
1669 * the transition), or a list of drivers that are notified about
1670 * changes in cpufreq policy.
1671 *
1672 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001673 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 */
1675int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1676{
1677 int ret;
1678
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001679 if (cpufreq_disabled())
1680 return -EINVAL;
1681
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001682 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 switch (list) {
1685 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001686 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001687 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 break;
1689 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001690 ret = blocking_notifier_chain_register(
1691 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 break;
1693 default:
1694 ret = -EINVAL;
1695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
1697 return ret;
1698}
1699EXPORT_SYMBOL(cpufreq_register_notifier);
1700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701/**
1702 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1703 * @nb: notifier block to be unregistered
Viresh Kumarbb176f72013-06-19 14:19:33 +05301704 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 *
1706 * Remove a driver from the CPU frequency notifier list.
1707 *
1708 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001709 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 */
1711int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1712{
1713 int ret;
1714
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001715 if (cpufreq_disabled())
1716 return -EINVAL;
1717
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 switch (list) {
1719 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001720 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001721 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 break;
1723 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001724 ret = blocking_notifier_chain_unregister(
1725 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 break;
1727 default:
1728 ret = -EINVAL;
1729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731 return ret;
1732}
1733EXPORT_SYMBOL(cpufreq_unregister_notifier);
1734
1735
1736/*********************************************************************
1737 * GOVERNORS *
1738 *********************************************************************/
1739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740int __cpufreq_driver_target(struct cpufreq_policy *policy,
1741 unsigned int target_freq,
1742 unsigned int relation)
1743{
1744 int retval = -EINVAL;
Viresh Kumar72499242012-10-31 01:28:21 +01001745 unsigned int old_target_freq = target_freq;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001746
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001747 if (cpufreq_disabled())
1748 return -ENODEV;
1749
Viresh Kumar72499242012-10-31 01:28:21 +01001750 /* Make sure that target_freq is within supported range */
1751 if (target_freq > policy->max)
1752 target_freq = policy->max;
1753 if (target_freq < policy->min)
1754 target_freq = policy->min;
1755
1756 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1757 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001758
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301759 /*
1760 * This might look like a redundant call as we are checking it again
1761 * after finding index. But it is left intentionally for cases where
1762 * exactly same freq is called again and so we can save on few function
1763 * calls.
1764 */
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001765 if (target_freq == policy->cur)
1766 return 0;
1767
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001768 if (cpufreq_driver->target)
1769 retval = cpufreq_driver->target(policy, target_freq, relation);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301770 else if (cpufreq_driver->target_index) {
1771 struct cpufreq_frequency_table *freq_table;
Viresh Kumard4019f02013-08-14 19:38:24 +05301772 struct cpufreq_freqs freqs;
1773 bool notify;
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301774 int index;
Ashok Raj90d45d12005-11-08 21:34:24 -08001775
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301776 freq_table = cpufreq_frequency_get_table(policy->cpu);
1777 if (unlikely(!freq_table)) {
1778 pr_err("%s: Unable to find freq_table\n", __func__);
1779 goto out;
1780 }
1781
1782 retval = cpufreq_frequency_table_target(policy, freq_table,
1783 target_freq, relation, &index);
1784 if (unlikely(retval)) {
1785 pr_err("%s: Unable to find matching freq\n", __func__);
1786 goto out;
1787 }
1788
Viresh Kumard4019f02013-08-14 19:38:24 +05301789 if (freq_table[index].frequency == policy->cur) {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301790 retval = 0;
Viresh Kumard4019f02013-08-14 19:38:24 +05301791 goto out;
1792 }
1793
1794 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
1795
1796 if (notify) {
1797 freqs.old = policy->cur;
1798 freqs.new = freq_table[index].frequency;
1799 freqs.flags = 0;
1800
1801 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1802 __func__, policy->cpu, freqs.old,
1803 freqs.new);
1804
1805 cpufreq_notify_transition(policy, &freqs,
1806 CPUFREQ_PRECHANGE);
1807 }
1808
1809 retval = cpufreq_driver->target_index(policy, index);
1810 if (retval)
1811 pr_err("%s: Failed to change cpu frequency: %d\n",
1812 __func__, retval);
1813
Viresh Kumarab1b1c42013-12-02 11:04:13 +05301814 if (notify)
1815 cpufreq_notify_post_transition(policy, &freqs, retval);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301816 }
1817
1818out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 return retval;
1820}
1821EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823int cpufreq_driver_target(struct cpufreq_policy *policy,
1824 unsigned int target_freq,
1825 unsigned int relation)
1826{
Julia Lawallf1829e42008-07-25 22:44:53 +02001827 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
viresh kumarad7722d2013-10-18 19:10:15 +05301829 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
1831 ret = __cpufreq_driver_target(policy, target_freq, relation);
1832
viresh kumarad7722d2013-10-18 19:10:15 +05301833 up_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 return ret;
1836}
1837EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1838
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001839/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001840 * when "event" is CPUFREQ_GOV_LIMITS
1841 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301843static int __cpufreq_governor(struct cpufreq_policy *policy,
1844 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845{
Dave Jonescc993ca2005-07-28 09:43:56 -07001846 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001847
1848 /* Only must be defined when default governor is known to have latency
1849 restrictions, like e.g. conservative or ondemand.
1850 That this is the case is already ensured in Kconfig
1851 */
1852#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1853 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1854#else
1855 struct cpufreq_governor *gov = NULL;
1856#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001857
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001858 /* Don't start any governor operations if we are entering suspend */
1859 if (cpufreq_suspended)
1860 return 0;
1861
Thomas Renninger1c256242007-10-02 13:28:12 -07001862 if (policy->governor->max_transition_latency &&
1863 policy->cpuinfo.transition_latency >
1864 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07001865 if (!gov)
1866 return -EINVAL;
1867 else {
1868 printk(KERN_WARNING "%s governor failed, too long"
1869 " transition latency of HW, fallback"
1870 " to %s governor\n",
1871 policy->governor->name,
1872 gov->name);
1873 policy->governor = gov;
1874 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Viresh Kumarfe492f32013-08-06 22:53:10 +05301877 if (event == CPUFREQ_GOV_POLICY_INIT)
1878 if (!try_module_get(policy->governor->owner))
1879 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001881 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301882 policy->cpu, event);
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001883
1884 mutex_lock(&cpufreq_governor_lock);
Srivatsa S. Bhat56d07db2013-09-07 01:23:55 +05301885 if ((policy->governor_enabled && event == CPUFREQ_GOV_START)
Viresh Kumarf73d3932013-08-31 17:53:40 +05301886 || (!policy->governor_enabled
1887 && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) {
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001888 mutex_unlock(&cpufreq_governor_lock);
1889 return -EBUSY;
1890 }
1891
1892 if (event == CPUFREQ_GOV_STOP)
1893 policy->governor_enabled = false;
1894 else if (event == CPUFREQ_GOV_START)
1895 policy->governor_enabled = true;
1896
1897 mutex_unlock(&cpufreq_governor_lock);
1898
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 ret = policy->governor->governor(policy, event);
1900
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001901 if (!ret) {
1902 if (event == CPUFREQ_GOV_POLICY_INIT)
1903 policy->governor->initialized++;
1904 else if (event == CPUFREQ_GOV_POLICY_EXIT)
1905 policy->governor->initialized--;
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001906 } else {
1907 /* Restore original values */
1908 mutex_lock(&cpufreq_governor_lock);
1909 if (event == CPUFREQ_GOV_STOP)
1910 policy->governor_enabled = true;
1911 else if (event == CPUFREQ_GOV_START)
1912 policy->governor_enabled = false;
1913 mutex_unlock(&cpufreq_governor_lock);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001914 }
Viresh Kumarb3940582013-02-01 05:42:58 +00001915
Viresh Kumarfe492f32013-08-06 22:53:10 +05301916 if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
1917 ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 module_put(policy->governor->owner);
1919
1920 return ret;
1921}
1922
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923int cpufreq_register_governor(struct cpufreq_governor *governor)
1924{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001925 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
1927 if (!governor)
1928 return -EINVAL;
1929
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001930 if (cpufreq_disabled())
1931 return -ENODEV;
1932
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001933 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001934
Viresh Kumarb3940582013-02-01 05:42:58 +00001935 governor->initialized = 0;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001936 err = -EBUSY;
1937 if (__find_governor(governor->name) == NULL) {
1938 err = 0;
1939 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Dave Jones32ee8c32006-02-28 00:43:23 -05001942 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001943 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944}
1945EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1948{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001949 int cpu;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 if (!governor)
1952 return;
1953
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001954 if (cpufreq_disabled())
1955 return;
1956
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001957 for_each_present_cpu(cpu) {
1958 if (cpu_online(cpu))
1959 continue;
1960 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1961 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1962 }
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001963
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001964 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001966 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 return;
1968}
1969EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1970
1971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972/*********************************************************************
1973 * POLICY INTERFACE *
1974 *********************************************************************/
1975
1976/**
1977 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05001978 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1979 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 *
1981 * Reads the current cpufreq policy.
1982 */
1983int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1984{
1985 struct cpufreq_policy *cpu_policy;
1986 if (!policy)
1987 return -EINVAL;
1988
1989 cpu_policy = cpufreq_cpu_get(cpu);
1990 if (!cpu_policy)
1991 return -EINVAL;
1992
Viresh Kumard5b73cd2013-08-06 22:53:06 +05301993 memcpy(policy, cpu_policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
1995 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 return 0;
1997}
1998EXPORT_SYMBOL(cpufreq_get_policy);
1999
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002000/*
Viresh Kumar037ce832013-10-02 14:13:16 +05302001 * policy : current policy.
2002 * new_policy: policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002003 */
Viresh Kumar037ce832013-10-02 14:13:16 +05302004static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302005 struct cpufreq_policy *new_policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006{
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002007 struct cpufreq_governor *old_gov;
2008 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302010 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", new_policy->cpu,
2011 new_policy->min, new_policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302013 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002015 if (new_policy->min > policy->max || new_policy->max < policy->min)
2016 return -EINVAL;
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02002017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 /* verify the cpu speed can be set within this limit */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302019 ret = cpufreq_driver->verify(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002021 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08002024 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302025 CPUFREQ_ADJUST, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
2027 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08002028 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302029 CPUFREQ_INCOMPATIBLE, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Viresh Kumarbb176f72013-06-19 14:19:33 +05302031 /*
2032 * verify the cpu speed can be set within this limit, which might be
2033 * different to the first one
2034 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302035 ret = cpufreq_driver->verify(new_policy);
Alan Sterne041c682006-03-27 01:16:30 -08002036 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002037 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
2039 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08002040 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302041 CPUFREQ_NOTIFY, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302043 policy->min = new_policy->min;
2044 policy->max = new_policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002046 pr_debug("new min and max freqs are %u - %u kHz\n",
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302047 policy->min, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002049 if (cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302050 policy->policy = new_policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002051 pr_debug("setting range\n");
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002052 return cpufreq_driver->setpolicy(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 }
2054
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002055 if (new_policy->governor == policy->governor)
2056 goto out;
2057
2058 pr_debug("governor switch\n");
2059
2060 /* save old, working values */
2061 old_gov = policy->governor;
2062 /* end old governor */
2063 if (old_gov) {
2064 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
2065 up_write(&policy->rwsem);
2066 __cpufreq_governor(policy,CPUFREQ_GOV_POLICY_EXIT);
2067 down_write(&policy->rwsem);
2068 }
2069
2070 /* start new governor */
2071 policy->governor = new_policy->governor;
2072 if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
2073 if (!__cpufreq_governor(policy, CPUFREQ_GOV_START))
2074 goto out;
2075
2076 up_write(&policy->rwsem);
2077 __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
2078 down_write(&policy->rwsem);
2079 }
2080
2081 /* new governor failed, so re-start old one */
2082 pr_debug("starting governor %s failed\n", policy->governor->name);
2083 if (old_gov) {
2084 policy->governor = old_gov;
2085 __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT);
2086 __cpufreq_governor(policy, CPUFREQ_GOV_START);
2087 }
2088
2089 return -EINVAL;
2090
2091 out:
2092 pr_debug("governor: change or update limits\n");
2093 return __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094}
2095
2096/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
2098 * @cpu: CPU which shall be re-evaluated
2099 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002100 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 * at different times.
2102 */
2103int cpufreq_update_policy(unsigned int cpu)
2104{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302105 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2106 struct cpufreq_policy new_policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02002107 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302109 if (!policy) {
Julia Lawallf1829e42008-07-25 22:44:53 +02002110 ret = -ENODEV;
2111 goto no_policy;
2112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
viresh kumarad7722d2013-10-18 19:10:15 +05302114 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002116 pr_debug("updating policy for CPU %u\n", cpu);
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302117 memcpy(&new_policy, policy, sizeof(*policy));
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302118 new_policy.min = policy->user_policy.min;
2119 new_policy.max = policy->user_policy.max;
2120 new_policy.policy = policy->user_policy.policy;
2121 new_policy.governor = policy->user_policy.governor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
Viresh Kumarbb176f72013-06-19 14:19:33 +05302123 /*
2124 * BIOS might change freq behind our back
2125 * -> ask driver for current freq and notify governors about a change
2126 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002127 if (cpufreq_driver->get) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302128 new_policy.cur = cpufreq_driver->get(cpu);
Viresh Kumarbd0fa9b2014-02-25 14:29:44 +05302129 if (WARN_ON(!new_policy.cur)) {
2130 ret = -EIO;
2131 goto no_policy;
2132 }
2133
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302134 if (!policy->cur) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002135 pr_debug("Driver did not initialize current freq");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302136 policy->cur = new_policy.cur;
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002137 } else {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302138 if (policy->cur != new_policy.cur && has_target())
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302139 cpufreq_out_of_sync(cpu, policy->cur,
2140 new_policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002141 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01002142 }
2143
Viresh Kumar037ce832013-10-02 14:13:16 +05302144 ret = cpufreq_set_policy(policy, &new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
viresh kumarad7722d2013-10-18 19:10:15 +05302146 up_write(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002147
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302148 cpufreq_cpu_put(policy);
Julia Lawallf1829e42008-07-25 22:44:53 +02002149no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 return ret;
2151}
2152EXPORT_SYMBOL(cpufreq_update_policy);
2153
Paul Gortmaker27609842013-06-19 13:54:04 -04002154static int cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002155 unsigned long action, void *hcpu)
2156{
2157 unsigned int cpu = (unsigned long)hcpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002158 struct device *dev;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302159 bool frozen = false;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002160
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002161 dev = get_cpu_device(cpu);
2162 if (dev) {
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302163
Rafael J. Wysockid4faadd2013-12-08 01:23:58 +01002164 if (action & CPU_TASKS_FROZEN)
2165 frozen = true;
2166
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302167 switch (action & ~CPU_TASKS_FROZEN) {
Ashok Rajc32b6b82005-10-30 14:59:54 -08002168 case CPU_ONLINE:
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302169 __cpufreq_add_dev(dev, NULL, frozen);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002170 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302171
Ashok Rajc32b6b82005-10-30 14:59:54 -08002172 case CPU_DOWN_PREPARE:
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05302173 __cpufreq_remove_dev_prepare(dev, NULL, frozen);
Srivatsa S. Bhat1aee40a2013-09-07 01:23:27 +05302174 break;
2175
2176 case CPU_POST_DEAD:
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05302177 __cpufreq_remove_dev_finish(dev, NULL, frozen);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002178 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302179
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002180 case CPU_DOWN_FAILED:
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302181 __cpufreq_add_dev(dev, NULL, frozen);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002182 break;
2183 }
2184 }
2185 return NOTIFY_OK;
2186}
2187
Neal Buckendahl9c36f742010-06-22 22:02:44 -05002188static struct notifier_block __refdata cpufreq_cpu_notifier = {
Viresh Kumarbb176f72013-06-19 14:19:33 +05302189 .notifier_call = cpufreq_cpu_callback,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002190};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
2192/*********************************************************************
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002193 * BOOST *
2194 *********************************************************************/
2195static int cpufreq_boost_set_sw(int state)
2196{
2197 struct cpufreq_frequency_table *freq_table;
2198 struct cpufreq_policy *policy;
2199 int ret = -EINVAL;
2200
2201 list_for_each_entry(policy, &cpufreq_policy_list, policy_list) {
2202 freq_table = cpufreq_frequency_get_table(policy->cpu);
2203 if (freq_table) {
2204 ret = cpufreq_frequency_table_cpuinfo(policy,
2205 freq_table);
2206 if (ret) {
2207 pr_err("%s: Policy frequency update failed\n",
2208 __func__);
2209 break;
2210 }
2211 policy->user_policy.max = policy->max;
2212 __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
2213 }
2214 }
2215
2216 return ret;
2217}
2218
2219int cpufreq_boost_trigger_state(int state)
2220{
2221 unsigned long flags;
2222 int ret = 0;
2223
2224 if (cpufreq_driver->boost_enabled == state)
2225 return 0;
2226
2227 write_lock_irqsave(&cpufreq_driver_lock, flags);
2228 cpufreq_driver->boost_enabled = state;
2229 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2230
2231 ret = cpufreq_driver->set_boost(state);
2232 if (ret) {
2233 write_lock_irqsave(&cpufreq_driver_lock, flags);
2234 cpufreq_driver->boost_enabled = !state;
2235 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2236
2237 pr_err("%s: Cannot %s BOOST\n", __func__,
2238 state ? "enable" : "disable");
2239 }
2240
2241 return ret;
2242}
2243
2244int cpufreq_boost_supported(void)
2245{
2246 if (likely(cpufreq_driver))
2247 return cpufreq_driver->boost_supported;
2248
2249 return 0;
2250}
2251EXPORT_SYMBOL_GPL(cpufreq_boost_supported);
2252
2253int cpufreq_boost_enabled(void)
2254{
2255 return cpufreq_driver->boost_enabled;
2256}
2257EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2258
2259/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2261 *********************************************************************/
2262
2263/**
2264 * cpufreq_register_driver - register a CPU Frequency driver
2265 * @driver_data: A struct cpufreq_driver containing the values#
2266 * submitted by the CPU Frequency driver.
2267 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302268 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05002270 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 *
2272 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002273int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274{
2275 unsigned long flags;
2276 int ret;
2277
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002278 if (cpufreq_disabled())
2279 return -ENODEV;
2280
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 if (!driver_data || !driver_data->verify || !driver_data->init ||
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302282 !(driver_data->setpolicy || driver_data->target_index ||
2283 driver_data->target))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 return -EINVAL;
2285
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002286 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
2288 if (driver_data->setpolicy)
2289 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2290
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002291 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002292 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002293 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Yinghai Lu4dea58062013-09-18 21:05:20 -07002294 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002296 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002297 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002299 if (cpufreq_boost_supported()) {
2300 /*
2301 * Check if driver provides function to enable boost -
2302 * if not, use cpufreq_boost_set_sw as default
2303 */
2304 if (!cpufreq_driver->set_boost)
2305 cpufreq_driver->set_boost = cpufreq_boost_set_sw;
2306
2307 ret = cpufreq_sysfs_create_file(&boost.attr);
2308 if (ret) {
2309 pr_err("%s: cannot register global BOOST sysfs file\n",
2310 __func__);
2311 goto err_null_driver;
2312 }
2313 }
2314
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002315 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002316 if (ret)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002317 goto err_boost_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002319 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 int i;
2321 ret = -ENODEV;
2322
2323 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07002324 for (i = 0; i < nr_cpu_ids; i++)
2325 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07002327 break;
2328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
2330 /* if all ->init() calls failed, unregister */
2331 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002332 pr_debug("no CPU initialized for driver %s\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05302333 driver_data->name);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002334 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 }
2336 }
2337
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002338 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002339 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002341 return 0;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002342err_if_unreg:
2343 subsys_interface_unregister(&cpufreq_interface);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002344err_boost_unreg:
2345 if (cpufreq_boost_supported())
2346 cpufreq_sysfs_remove_file(&boost.attr);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002347err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002348 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002349 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002350 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05002351 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352}
2353EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2354
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355/**
2356 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2357 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302358 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 * the right to do so, i.e. if you have succeeded in initialising before!
2360 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2361 * currently not initialised.
2362 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002363int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364{
2365 unsigned long flags;
2366
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002367 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002370 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002372 subsys_interface_unregister(&cpufreq_interface);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002373 if (cpufreq_boost_supported())
2374 cpufreq_sysfs_remove_file(&boost.attr);
2375
Chandra Seetharaman65edc682006-06-27 02:54:08 -07002376 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Viresh Kumar6eed9402013-08-06 22:53:11 +05302378 down_write(&cpufreq_rwsem);
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002379 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302380
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002381 cpufreq_driver = NULL;
Viresh Kumar6eed9402013-08-06 22:53:11 +05302382
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002383 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302384 up_write(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
2386 return 0;
2387}
2388EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002389
2390static int __init cpufreq_core_init(void)
2391{
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002392 if (cpufreq_disabled())
2393 return -ENODEV;
2394
Viresh Kumar2361be22013-05-17 16:09:09 +05302395 cpufreq_global_kobject = kobject_create();
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002396 BUG_ON(!cpufreq_global_kobject);
2397
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002398 return 0;
2399}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002400core_initcall(cpufreq_core_init);