blob: 37f30550522a90595b945f786b154ff1dfb5eed6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/cpufreq/cpufreq.c
3 *
4 * Copyright (C) 2001 Russell King
5 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
Viresh Kumarbb176f72013-06-19 14:19:33 +05306 * (C) 2013 Viresh Kumar <viresh.kumar@linaro.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Ashok Rajc32b6b82005-10-30 14:59:54 -08008 * Oct 2005 - Ashok Raj <ashok.raj@intel.com>
Dave Jones32ee8c32006-02-28 00:43:23 -05009 * Added handling for CPU hotplug
Dave Jones8ff69732006-03-05 03:37:23 -050010 * Feb 2006 - Jacob Shin <jacob.shin@amd.com>
11 * Fix handling for CPU hotplug -- affected CPUs
Ashok Rajc32b6b82005-10-30 14:59:54 -080012 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Viresh Kumardb701152012-10-23 01:29:03 +020018#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
Viresh Kumar5ff0a262013-08-06 22:53:03 +053020#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/cpufreq.h>
22#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/device.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053024#include <linux/init.h>
25#include <linux/kernel_stat.h>
26#include <linux/module.h>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080027#include <linux/mutex.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053028#include <linux/slab.h>
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +010029#include <linux/syscore_ops.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053030#include <linux/tick.h>
Thomas Renninger6f4f2722010-04-20 13:17:36 +020031#include <trace/events/power.h>
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/**
Dave Jonescd878472006-08-11 17:59:28 -040034 * The "cpufreq driver" - the arch- or hardware-dependent low
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 * level driver of CPUFreq support, and its spinlock. This lock
36 * also protects the cpufreq_cpu_data array.
37 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +020038static struct cpufreq_driver *cpufreq_driver;
Mike Travis7a6aedf2008-03-25 15:06:53 -070039static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +053040static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data_fallback);
Viresh Kumarbb176f72013-06-19 14:19:33 +053041static DEFINE_RWLOCK(cpufreq_driver_lock);
Jane Li6f1e4ef2014-01-03 17:17:41 +080042DEFINE_MUTEX(cpufreq_governor_lock);
Lukasz Majewskic88a1f82013-08-06 22:53:08 +053043static LIST_HEAD(cpufreq_policy_list);
Viresh Kumarbb176f72013-06-19 14:19:33 +053044
Thomas Renninger084f3492007-07-09 11:35:28 -070045#ifdef CONFIG_HOTPLUG_CPU
46/* This one keeps track of the previously set governor of a removed CPU */
Dmitry Monakhove77b89f2009-10-05 00:38:55 +040047static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
Thomas Renninger084f3492007-07-09 11:35:28 -070048#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +053050static inline bool has_target(void)
51{
52 return cpufreq_driver->target_index || cpufreq_driver->target;
53}
54
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080055/*
Viresh Kumar6eed9402013-08-06 22:53:11 +053056 * rwsem to guarantee that cpufreq driver module doesn't unload during critical
57 * sections
58 */
59static DECLARE_RWSEM(cpufreq_rwsem);
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/* internal prototypes */
Dave Jones29464f22009-01-18 01:37:11 -050062static int __cpufreq_governor(struct cpufreq_policy *policy,
63 unsigned int event);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080064static unsigned int __cpufreq_get(unsigned int cpu);
David Howells65f27f32006-11-22 14:55:48 +000065static void handle_update(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67/**
Dave Jones32ee8c32006-02-28 00:43:23 -050068 * Two notifier lists: the "policy" list is involved in the
69 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 * "transition" list for kernel code that needs to handle
71 * changes to devices when the CPU clock speed changes.
72 * The mutex locks both lists.
73 */
Alan Sterne041c682006-03-27 01:16:30 -080074static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -070075static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -020077static bool init_cpufreq_transition_notifier_list_called;
Alan Sternb4dfdbb2006-10-04 02:17:06 -070078static int __init init_cpufreq_transition_notifier_list(void)
79{
80 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -020081 init_cpufreq_transition_notifier_list_called = true;
Alan Sternb4dfdbb2006-10-04 02:17:06 -070082 return 0;
83}
Linus Torvaldsb3438f82006-11-20 11:47:18 -080084pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -040086static int off __read_mostly;
Viresh Kumarda584452012-10-26 00:51:32 +020087static int cpufreq_disabled(void)
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -040088{
89 return off;
90}
91void disable_cpufreq(void)
92{
93 off = 1;
94}
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static LIST_HEAD(cpufreq_governor_list);
Dave Jones29464f22009-01-18 01:37:11 -050096static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000098bool have_governor_per_policy(void)
99{
Viresh Kumar0b981e72013-10-02 14:13:18 +0530100 return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000101}
Viresh Kumar3f869d62013-05-16 05:09:56 +0000102EXPORT_SYMBOL_GPL(have_governor_per_policy);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000103
Viresh Kumar944e9a02013-05-16 05:09:57 +0000104struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
105{
106 if (have_governor_per_policy())
107 return &policy->kobj;
108 else
109 return cpufreq_global_kobject;
110}
111EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
112
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000113static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
114{
115 u64 idle_time;
116 u64 cur_wall_time;
117 u64 busy_time;
118
119 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
120
121 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
122 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
123 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
124 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
125 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
126 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
127
128 idle_time = cur_wall_time - busy_time;
129 if (wall)
130 *wall = cputime_to_usecs(cur_wall_time);
131
132 return cputime_to_usecs(idle_time);
133}
134
135u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
136{
137 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
138
139 if (idle_time == -1ULL)
140 return get_cpu_idle_time_jiffy(cpu, wall);
141 else if (!io_busy)
142 idle_time += get_cpu_iowait_time_us(cpu, wall);
143
144 return idle_time;
145}
146EXPORT_SYMBOL_GPL(get_cpu_idle_time);
147
Viresh Kumar70e9e772013-10-03 20:29:07 +0530148/*
149 * This is a generic cpufreq init() routine which can be used by cpufreq
150 * drivers of SMP systems. It will do following:
151 * - validate & show freq table passed
152 * - set policies transition latency
153 * - policy->cpus with all possible CPUs
154 */
155int cpufreq_generic_init(struct cpufreq_policy *policy,
156 struct cpufreq_frequency_table *table,
157 unsigned int transition_latency)
158{
159 int ret;
160
161 ret = cpufreq_table_validate_and_show(policy, table);
162 if (ret) {
163 pr_err("%s: invalid frequency table: %d\n", __func__, ret);
164 return ret;
165 }
166
167 policy->cpuinfo.transition_latency = transition_latency;
168
169 /*
170 * The driver only supports the SMP configuartion where all processors
171 * share the clock and voltage and clock.
172 */
173 cpumask_setall(policy->cpus);
174
175 return 0;
176}
177EXPORT_SYMBOL_GPL(cpufreq_generic_init);
178
Viresh Kumar652ed952014-01-09 20:38:43 +0530179unsigned int cpufreq_generic_get(unsigned int cpu)
180{
181 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
182
183 if (!policy || IS_ERR(policy->clk)) {
184 pr_err("%s: No %s associated to cpu: %d\n", __func__,
185 policy ? "clk" : "policy", cpu);
186 return 0;
187 }
188
189 return clk_get_rate(policy->clk) / 1000;
190}
191EXPORT_SYMBOL_GPL(cpufreq_generic_get);
192
Viresh Kumar6eed9402013-08-06 22:53:11 +0530193struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530195 struct cpufreq_policy *policy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 unsigned long flags;
197
Viresh Kumar6eed9402013-08-06 22:53:11 +0530198 if (cpufreq_disabled() || (cpu >= nr_cpu_ids))
199 return NULL;
200
201 if (!down_read_trylock(&cpufreq_rwsem))
202 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 /* get the cpufreq driver */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000205 read_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Viresh Kumar6eed9402013-08-06 22:53:11 +0530207 if (cpufreq_driver) {
208 /* get the CPU */
209 policy = per_cpu(cpufreq_cpu_data, cpu);
210 if (policy)
211 kobject_get(&policy->kobj);
212 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200213
Viresh Kumar6eed9402013-08-06 22:53:11 +0530214 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530216 if (!policy)
Viresh Kumar6eed9402013-08-06 22:53:11 +0530217 up_read(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530219 return policy;
Stephen Boyda9144432012-07-20 18:14:38 +0000220}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
222
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530223void cpufreq_cpu_put(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000225 if (cpufreq_disabled())
226 return;
227
Viresh Kumar6eed9402013-08-06 22:53:11 +0530228 kobject_put(&policy->kobj);
229 up_read(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
235 *********************************************************************/
236
237/**
238 * adjust_jiffies - adjust the system "loops_per_jiffy"
239 *
240 * This function alters the system "loops_per_jiffy" for the clock
241 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500242 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 * per-CPU loops_per_jiffy value wherever possible.
244 */
245#ifndef CONFIG_SMP
246static unsigned long l_p_j_ref;
Viresh Kumarbb176f72013-06-19 14:19:33 +0530247static unsigned int l_p_j_ref_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Arjan van de Ven858119e2006-01-14 13:20:43 -0800249static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 if (ci->flags & CPUFREQ_CONST_LOOPS)
252 return;
253
254 if (!l_p_j_ref_freq) {
255 l_p_j_ref = loops_per_jiffy;
256 l_p_j_ref_freq = ci->old;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200257 pr_debug("saving %lu as reference value for loops_per_jiffy; "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530258 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
Viresh Kumarbb176f72013-06-19 14:19:33 +0530260 if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700261 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530262 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
263 ci->new);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200264 pr_debug("scaling loops_per_jiffy to %lu "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530265 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267}
268#else
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530269static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
270{
271 return;
272}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273#endif
274
Viresh Kumar0956df9c2013-06-19 14:19:34 +0530275static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530276 struct cpufreq_freqs *freqs, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 BUG_ON(irqs_disabled());
279
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000280 if (cpufreq_disabled())
281 return;
282
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200283 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200284 pr_debug("notification %u of frequency transition to %u kHz\n",
Dave Jonese4472cb2006-01-31 15:53:55 -0800285 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500290 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800291 * which is not equal to what the cpufreq core thinks is
292 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200294 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800295 if ((policy) && (policy->cpu == freqs->cpu) &&
296 (policy->cur) && (policy->cur != freqs->old)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200297 pr_debug("Warning: CPU frequency is"
Dave Jonese4472cb2006-01-31 15:53:55 -0800298 " %u, cpufreq assumed %u kHz.\n",
299 freqs->old, policy->cur);
300 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700303 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800304 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
306 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 case CPUFREQ_POSTCHANGE:
309 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200310 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
Thomas Renninger6f4f2722010-04-20 13:17:36 +0200311 (unsigned long)freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100312 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700313 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800314 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800315 if (likely(policy) && likely(policy->cpu == freqs->cpu))
316 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 break;
318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
Viresh Kumarbb176f72013-06-19 14:19:33 +0530320
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530321/**
322 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
323 * on frequency transition.
324 *
325 * This function calls the transition notifiers and the "adjust_jiffies"
326 * function. It is called twice on all CPU frequency changes that have
327 * external effects.
328 */
329void cpufreq_notify_transition(struct cpufreq_policy *policy,
330 struct cpufreq_freqs *freqs, unsigned int state)
331{
332 for_each_cpu(freqs->cpu, policy->cpus)
333 __cpufreq_notify_transition(policy, freqs, state);
334}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
336
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530337/* Do post notifications when there are chances that transition has failed */
338void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
339 struct cpufreq_freqs *freqs, int transition_failed)
340{
341 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
342 if (!transition_failed)
343 return;
344
345 swap(freqs->old, freqs->new);
346 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
347 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
348}
349EXPORT_SYMBOL_GPL(cpufreq_notify_post_transition);
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352/*********************************************************************
353 * SYSFS INTERFACE *
354 *********************************************************************/
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100355ssize_t show_boost(struct kobject *kobj,
356 struct attribute *attr, char *buf)
357{
358 return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
359}
360
361static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
362 const char *buf, size_t count)
363{
364 int ret, enable;
365
366 ret = sscanf(buf, "%d", &enable);
367 if (ret != 1 || enable < 0 || enable > 1)
368 return -EINVAL;
369
370 if (cpufreq_boost_trigger_state(enable)) {
371 pr_err("%s: Cannot %s BOOST!\n", __func__,
372 enable ? "enable" : "disable");
373 return -EINVAL;
374 }
375
376 pr_debug("%s: cpufreq BOOST %s\n", __func__,
377 enable ? "enabled" : "disabled");
378
379 return count;
380}
381define_one_global_rw(boost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700383static struct cpufreq_governor *__find_governor(const char *str_governor)
384{
385 struct cpufreq_governor *t;
386
387 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
Dave Jones29464f22009-01-18 01:37:11 -0500388 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700389 return t;
390
391 return NULL;
392}
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394/**
395 * cpufreq_parse_governor - parse a governor string
396 */
Dave Jones905d77c2008-03-05 14:28:32 -0500397static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 struct cpufreq_governor **governor)
399{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700400 int err = -EINVAL;
401
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200402 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700403 goto out;
404
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200405 if (cpufreq_driver->setpolicy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
407 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700408 err = 0;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530409 } else if (!strnicmp(str_governor, "powersave",
410 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700412 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530414 } else if (has_target()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700416
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800417 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700418
419 t = __find_governor(str_governor);
420
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700421 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700422 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700423
Kees Cook1a8e1462011-05-04 08:38:56 -0700424 mutex_unlock(&cpufreq_governor_mutex);
425 ret = request_module("cpufreq_%s", str_governor);
426 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700427
Kees Cook1a8e1462011-05-04 08:38:56 -0700428 if (ret == 0)
429 t = __find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700430 }
431
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700432 if (t != NULL) {
433 *governor = t;
434 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700436
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800437 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
Dave Jones29464f22009-01-18 01:37:11 -0500439out:
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700440 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530444 * cpufreq_per_cpu_attr_read() / show_##file_name() -
445 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 *
447 * Write out information from cpufreq_driver->policy[cpu]; object must be
448 * "unsigned int".
449 */
450
Dave Jones32ee8c32006-02-28 00:43:23 -0500451#define show_one(file_name, object) \
452static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500453(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500454{ \
Dave Jones29464f22009-01-18 01:37:11 -0500455 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
458show_one(cpuinfo_min_freq, cpuinfo.min_freq);
459show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100460show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461show_one(scaling_min_freq, min);
462show_one(scaling_max_freq, max);
463show_one(scaling_cur_freq, cur);
464
Viresh Kumar037ce832013-10-02 14:13:16 +0530465static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530466 struct cpufreq_policy *new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468/**
469 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
470 */
471#define store_one(file_name, object) \
472static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500473(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{ \
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530475 int ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 struct cpufreq_policy new_policy; \
477 \
478 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
479 if (ret) \
480 return -EINVAL; \
481 \
Dave Jones29464f22009-01-18 01:37:11 -0500482 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (ret != 1) \
484 return -EINVAL; \
485 \
Viresh Kumar037ce832013-10-02 14:13:16 +0530486 ret = cpufreq_set_policy(policy, &new_policy); \
Thomas Renninger7970e082006-04-13 15:14:04 +0200487 policy->user_policy.object = policy->object; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 \
489 return ret ? ret : count; \
490}
491
Dave Jones29464f22009-01-18 01:37:11 -0500492store_one(scaling_min_freq, min);
493store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495/**
496 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
497 */
Dave Jones905d77c2008-03-05 14:28:32 -0500498static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
499 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800501 unsigned int cur_freq = __cpufreq_get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (!cur_freq)
503 return sprintf(buf, "<unknown>");
504 return sprintf(buf, "%u\n", cur_freq);
505}
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507/**
508 * show_scaling_governor - show the current policy for the specified CPU
509 */
Dave Jones905d77c2008-03-05 14:28:32 -0500510static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Dave Jones29464f22009-01-18 01:37:11 -0500512 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return sprintf(buf, "powersave\n");
514 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
515 return sprintf(buf, "performance\n");
516 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200517 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500518 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return -EINVAL;
520}
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522/**
523 * store_scaling_governor - store policy for the specified CPU
524 */
Dave Jones905d77c2008-03-05 14:28:32 -0500525static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
526 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530528 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 char str_governor[16];
530 struct cpufreq_policy new_policy;
531
532 ret = cpufreq_get_policy(&new_policy, policy->cpu);
533 if (ret)
534 return ret;
535
Dave Jones29464f22009-01-18 01:37:11 -0500536 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (ret != 1)
538 return -EINVAL;
539
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530540 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
541 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return -EINVAL;
543
Viresh Kumar037ce832013-10-02 14:13:16 +0530544 ret = cpufreq_set_policy(policy, &new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200545
546 policy->user_policy.policy = policy->policy;
547 policy->user_policy.governor = policy->governor;
Thomas Renninger7970e082006-04-13 15:14:04 +0200548
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530549 if (ret)
550 return ret;
551 else
552 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
555/**
556 * show_scaling_driver - show the cpufreq driver currently loaded
557 */
Dave Jones905d77c2008-03-05 14:28:32 -0500558static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200560 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
563/**
564 * show_scaling_available_governors - show the available CPUfreq governors
565 */
Dave Jones905d77c2008-03-05 14:28:32 -0500566static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
567 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 ssize_t i = 0;
570 struct cpufreq_governor *t;
571
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530572 if (!has_target()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 i += sprintf(buf, "performance powersave");
574 goto out;
575 }
576
577 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
Dave Jones29464f22009-01-18 01:37:11 -0500578 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
579 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200581 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500583out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 i += sprintf(&buf[i], "\n");
585 return i;
586}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700587
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800588ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 ssize_t i = 0;
591 unsigned int cpu;
592
Rusty Russell835481d2009-01-04 05:18:06 -0800593 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (i)
595 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
596 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
597 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500598 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
600 i += sprintf(&buf[i], "\n");
601 return i;
602}
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800603EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700605/**
606 * show_related_cpus - show the CPUs affected by each transition even if
607 * hw coordination is in use
608 */
609static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
610{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800611 return cpufreq_show_cpus(policy->related_cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700612}
613
614/**
615 * show_affected_cpus - show the CPUs affected by each transition
616 */
617static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
618{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800619 return cpufreq_show_cpus(policy->cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700620}
621
Venki Pallipadi9e769882007-10-26 10:18:21 -0700622static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500623 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700624{
625 unsigned int freq = 0;
626 unsigned int ret;
627
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700628 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700629 return -EINVAL;
630
631 ret = sscanf(buf, "%u", &freq);
632 if (ret != 1)
633 return -EINVAL;
634
635 policy->governor->store_setspeed(policy, freq);
636
637 return count;
638}
639
640static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
641{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700642 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700643 return sprintf(buf, "<unsupported>\n");
644
645 return policy->governor->show_setspeed(policy, buf);
646}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Thomas Renningere2f74f32009-11-19 12:31:01 +0100648/**
viresh kumar8bf1ac722012-10-23 01:23:33 +0200649 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100650 */
651static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
652{
653 unsigned int limit;
654 int ret;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200655 if (cpufreq_driver->bios_limit) {
656 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
Thomas Renningere2f74f32009-11-19 12:31:01 +0100657 if (!ret)
658 return sprintf(buf, "%u\n", limit);
659 }
660 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
661}
662
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200663cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
664cpufreq_freq_attr_ro(cpuinfo_min_freq);
665cpufreq_freq_attr_ro(cpuinfo_max_freq);
666cpufreq_freq_attr_ro(cpuinfo_transition_latency);
667cpufreq_freq_attr_ro(scaling_available_governors);
668cpufreq_freq_attr_ro(scaling_driver);
669cpufreq_freq_attr_ro(scaling_cur_freq);
670cpufreq_freq_attr_ro(bios_limit);
671cpufreq_freq_attr_ro(related_cpus);
672cpufreq_freq_attr_ro(affected_cpus);
673cpufreq_freq_attr_rw(scaling_min_freq);
674cpufreq_freq_attr_rw(scaling_max_freq);
675cpufreq_freq_attr_rw(scaling_governor);
676cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Dave Jones905d77c2008-03-05 14:28:32 -0500678static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 &cpuinfo_min_freq.attr,
680 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100681 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 &scaling_min_freq.attr,
683 &scaling_max_freq.attr,
684 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700685 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 &scaling_governor.attr,
687 &scaling_driver.attr,
688 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700689 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 NULL
691};
692
Dave Jones29464f22009-01-18 01:37:11 -0500693#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
694#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Dave Jones29464f22009-01-18 01:37:11 -0500696static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Dave Jones905d77c2008-03-05 14:28:32 -0500698 struct cpufreq_policy *policy = to_policy(kobj);
699 struct freq_attr *fattr = to_attr(attr);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530700 ssize_t ret;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530701
702 if (!down_read_trylock(&cpufreq_rwsem))
Viresh Kumar1b750e32013-10-02 14:13:09 +0530703 return -EINVAL;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800704
viresh kumarad7722d2013-10-18 19:10:15 +0530705 down_read(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800706
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530707 if (fattr->show)
708 ret = fattr->show(policy, buf);
709 else
710 ret = -EIO;
711
viresh kumarad7722d2013-10-18 19:10:15 +0530712 up_read(&policy->rwsem);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530713 up_read(&cpufreq_rwsem);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return ret;
716}
717
Dave Jones905d77c2008-03-05 14:28:32 -0500718static ssize_t store(struct kobject *kobj, struct attribute *attr,
719 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Dave Jones905d77c2008-03-05 14:28:32 -0500721 struct cpufreq_policy *policy = to_policy(kobj);
722 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500723 ssize_t ret = -EINVAL;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530724
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530725 get_online_cpus();
726
727 if (!cpu_online(policy->cpu))
728 goto unlock;
729
Viresh Kumar6eed9402013-08-06 22:53:11 +0530730 if (!down_read_trylock(&cpufreq_rwsem))
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530731 goto unlock;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800732
viresh kumarad7722d2013-10-18 19:10:15 +0530733 down_write(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800734
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530735 if (fattr->store)
736 ret = fattr->store(policy, buf, count);
737 else
738 ret = -EIO;
739
viresh kumarad7722d2013-10-18 19:10:15 +0530740 up_write(&policy->rwsem);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530741
Viresh Kumar6eed9402013-08-06 22:53:11 +0530742 up_read(&cpufreq_rwsem);
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530743unlock:
744 put_online_cpus();
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return ret;
747}
748
Dave Jones905d77c2008-03-05 14:28:32 -0500749static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Dave Jones905d77c2008-03-05 14:28:32 -0500751 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200752 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 complete(&policy->kobj_unregister);
754}
755
Emese Revfy52cf25d2010-01-19 02:58:23 +0100756static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 .show = show,
758 .store = store,
759};
760
761static struct kobj_type ktype_cpufreq = {
762 .sysfs_ops = &sysfs_ops,
763 .default_attrs = default_attrs,
764 .release = cpufreq_sysfs_release,
765};
766
Viresh Kumar2361be22013-05-17 16:09:09 +0530767struct kobject *cpufreq_global_kobject;
768EXPORT_SYMBOL(cpufreq_global_kobject);
769
770static int cpufreq_global_kobject_usage;
771
772int cpufreq_get_global_kobject(void)
773{
774 if (!cpufreq_global_kobject_usage++)
775 return kobject_add(cpufreq_global_kobject,
776 &cpu_subsys.dev_root->kobj, "%s", "cpufreq");
777
778 return 0;
779}
780EXPORT_SYMBOL(cpufreq_get_global_kobject);
781
782void cpufreq_put_global_kobject(void)
783{
784 if (!--cpufreq_global_kobject_usage)
785 kobject_del(cpufreq_global_kobject);
786}
787EXPORT_SYMBOL(cpufreq_put_global_kobject);
788
789int cpufreq_sysfs_create_file(const struct attribute *attr)
790{
791 int ret = cpufreq_get_global_kobject();
792
793 if (!ret) {
794 ret = sysfs_create_file(cpufreq_global_kobject, attr);
795 if (ret)
796 cpufreq_put_global_kobject();
797 }
798
799 return ret;
800}
801EXPORT_SYMBOL(cpufreq_sysfs_create_file);
802
803void cpufreq_sysfs_remove_file(const struct attribute *attr)
804{
805 sysfs_remove_file(cpufreq_global_kobject, attr);
806 cpufreq_put_global_kobject();
807}
808EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
809
Dave Jones19d6f7e2009-07-08 17:35:39 -0400810/* symlink affected CPUs */
Viresh Kumar308b60e2013-07-31 14:35:14 +0200811static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400812{
813 unsigned int j;
814 int ret = 0;
815
816 for_each_cpu(j, policy->cpus) {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800817 struct device *cpu_dev;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400818
Viresh Kumar308b60e2013-07-31 14:35:14 +0200819 if (j == policy->cpu)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400820 continue;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400821
Viresh Kumare8fdde12013-07-31 14:31:33 +0200822 pr_debug("Adding link for CPU: %u\n", j);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800823 cpu_dev = get_cpu_device(j);
824 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Dave Jones19d6f7e2009-07-08 17:35:39 -0400825 "cpufreq");
Rafael J. Wysocki71c34612013-08-04 01:19:34 +0200826 if (ret)
827 break;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400828 }
829 return ret;
830}
831
Viresh Kumar308b60e2013-07-31 14:35:14 +0200832static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800833 struct device *dev)
Dave Jones909a6942009-07-08 18:05:42 -0400834{
835 struct freq_attr **drv_attr;
Dave Jones909a6942009-07-08 18:05:42 -0400836 int ret = 0;
Dave Jones909a6942009-07-08 18:05:42 -0400837
838 /* prepare interface data */
839 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800840 &dev->kobj, "cpufreq");
Dave Jones909a6942009-07-08 18:05:42 -0400841 if (ret)
842 return ret;
843
844 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200845 drv_attr = cpufreq_driver->attr;
Dave Jones909a6942009-07-08 18:05:42 -0400846 while ((drv_attr) && (*drv_attr)) {
847 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
848 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200849 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400850 drv_attr++;
851 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200852 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -0400853 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
854 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200855 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400856 }
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530857 if (has_target()) {
Dave Jones909a6942009-07-08 18:05:42 -0400858 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
859 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200860 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400861 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200862 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +0100863 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
864 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200865 goto err_out_kobj_put;
Thomas Renningere2f74f32009-11-19 12:31:01 +0100866 }
Dave Jones909a6942009-07-08 18:05:42 -0400867
Viresh Kumar308b60e2013-07-31 14:35:14 +0200868 ret = cpufreq_add_dev_symlink(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400869 if (ret)
870 goto err_out_kobj_put;
871
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530872 return ret;
873
874err_out_kobj_put:
875 kobject_put(&policy->kobj);
876 wait_for_completion(&policy->kobj_unregister);
877 return ret;
878}
879
880static void cpufreq_init_policy(struct cpufreq_policy *policy)
881{
882 struct cpufreq_policy new_policy;
883 int ret = 0;
884
Viresh Kumard5b73cd2013-08-06 22:53:06 +0530885 memcpy(&new_policy, policy, sizeof(*policy));
Jason Barona27a9ab2013-12-19 22:50:50 +0000886
887 /* Use the default policy if its valid. */
888 if (cpufreq_driver->setpolicy)
889 cpufreq_parse_governor(policy->governor->name,
890 &new_policy.policy, NULL);
891
Viresh Kumar037ce832013-10-02 14:13:16 +0530892 /* assure that the starting sequence is run in cpufreq_set_policy */
Dave Jonesecf7e462009-07-08 18:48:47 -0400893 policy->governor = NULL;
894
895 /* set default policy */
Viresh Kumar037ce832013-10-02 14:13:16 +0530896 ret = cpufreq_set_policy(policy, &new_policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400897 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200898 pr_debug("setting policy failed\n");
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200899 if (cpufreq_driver->exit)
900 cpufreq_driver->exit(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400901 }
Dave Jones909a6942009-07-08 18:05:42 -0400902}
903
Viresh Kumarfcf80582013-01-29 14:39:08 +0000904#ifdef CONFIG_HOTPLUG_CPU
Viresh Kumard8d3b472013-08-04 01:20:07 +0200905static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,
Viresh Kumar42f921a2013-12-20 21:26:02 +0530906 unsigned int cpu, struct device *dev)
Viresh Kumarfcf80582013-01-29 14:39:08 +0000907{
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530908 int ret = 0;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000909 unsigned long flags;
910
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530911 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +0530912 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
913 if (ret) {
914 pr_err("%s: Failed to stop governor\n", __func__);
915 return ret;
916 }
917 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000918
viresh kumarad7722d2013-10-18 19:10:15 +0530919 down_write(&policy->rwsem);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530920
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000921 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530922
Viresh Kumarfcf80582013-01-29 14:39:08 +0000923 cpumask_set_cpu(cpu, policy->cpus);
924 per_cpu(cpufreq_cpu_data, cpu) = policy;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000925 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000926
viresh kumarad7722d2013-10-18 19:10:15 +0530927 up_write(&policy->rwsem);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530928
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530929 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +0530930 if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
931 (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
932 pr_err("%s: Failed to start governor\n", __func__);
933 return ret;
934 }
Viresh Kumar820c6ca2013-04-22 00:48:03 +0200935 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000936
Viresh Kumar42f921a2013-12-20 21:26:02 +0530937 return sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
Viresh Kumarfcf80582013-01-29 14:39:08 +0000938}
939#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530941static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu)
942{
943 struct cpufreq_policy *policy;
944 unsigned long flags;
945
Lan Tianyu44871c92013-09-11 15:05:05 +0800946 read_lock_irqsave(&cpufreq_driver_lock, flags);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530947
948 policy = per_cpu(cpufreq_cpu_data_fallback, cpu);
949
Lan Tianyu44871c92013-09-11 15:05:05 +0800950 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530951
952 return policy;
953}
954
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +0530955static struct cpufreq_policy *cpufreq_policy_alloc(void)
956{
957 struct cpufreq_policy *policy;
958
959 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
960 if (!policy)
961 return NULL;
962
963 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
964 goto err_free_policy;
965
966 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
967 goto err_free_cpumask;
968
Lukasz Majewskic88a1f82013-08-06 22:53:08 +0530969 INIT_LIST_HEAD(&policy->policy_list);
viresh kumarad7722d2013-10-18 19:10:15 +0530970 init_rwsem(&policy->rwsem);
971
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +0530972 return policy;
973
974err_free_cpumask:
975 free_cpumask_var(policy->cpus);
976err_free_policy:
977 kfree(policy);
978
979 return NULL;
980}
981
Viresh Kumar42f921a2013-12-20 21:26:02 +0530982static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
983{
984 struct kobject *kobj;
985 struct completion *cmp;
986
Viresh Kumarfcd7af92014-01-07 07:10:10 +0530987 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
988 CPUFREQ_REMOVE_POLICY, policy);
989
Viresh Kumar42f921a2013-12-20 21:26:02 +0530990 down_read(&policy->rwsem);
991 kobj = &policy->kobj;
992 cmp = &policy->kobj_unregister;
993 up_read(&policy->rwsem);
994 kobject_put(kobj);
995
996 /*
997 * We need to make sure that the underlying kobj is
998 * actually not referenced anymore by anybody before we
999 * proceed with unloading.
1000 */
1001 pr_debug("waiting for dropping of refcount\n");
1002 wait_for_completion(cmp);
1003 pr_debug("wait complete\n");
1004}
1005
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301006static void cpufreq_policy_free(struct cpufreq_policy *policy)
1007{
1008 free_cpumask_var(policy->related_cpus);
1009 free_cpumask_var(policy->cpus);
1010 kfree(policy);
1011}
1012
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301013static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
1014{
Srivatsa S. Bhat99ec8992013-09-12 17:29:09 +05301015 if (WARN_ON(cpu == policy->cpu))
Srivatsa S. Bhatcb38ed52013-09-12 01:43:42 +05301016 return;
1017
viresh kumarad7722d2013-10-18 19:10:15 +05301018 down_write(&policy->rwsem);
Viresh Kumar8efd5762013-09-17 10:22:11 +05301019
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301020 policy->last_cpu = policy->cpu;
1021 policy->cpu = cpu;
1022
viresh kumarad7722d2013-10-18 19:10:15 +05301023 up_write(&policy->rwsem);
Viresh Kumar8efd5762013-09-17 10:22:11 +05301024
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301025 cpufreq_frequency_table_update_policy_cpu(policy);
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301026 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1027 CPUFREQ_UPDATE_POLICY_CPU, policy);
1028}
1029
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301030static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif,
1031 bool frozen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
Viresh Kumarfcf80582013-01-29 14:39:08 +00001033 unsigned int j, cpu = dev->id;
Viresh Kumar65922462013-02-07 10:56:03 +05301034 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 unsigned long flags;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001037#ifdef CONFIG_HOTPLUG_CPU
Viresh Kumar1b274292013-08-20 12:08:26 +05301038 struct cpufreq_policy *tpolicy;
Viresh Kumarfcf80582013-01-29 14:39:08 +00001039 struct cpufreq_governor *gov;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001040#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Ashok Rajc32b6b82005-10-30 14:59:54 -08001042 if (cpu_is_offline(cpu))
1043 return 0;
1044
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001045 pr_debug("adding CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047#ifdef CONFIG_SMP
1048 /* check whether a different CPU already registered this
1049 * CPU because it is in the same boat. */
1050 policy = cpufreq_cpu_get(cpu);
1051 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -05001052 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 return 0;
1054 }
Li Zhong5025d622013-08-21 01:31:08 +02001055#endif
Viresh Kumarfcf80582013-01-29 14:39:08 +00001056
Viresh Kumar6eed9402013-08-06 22:53:11 +05301057 if (!down_read_trylock(&cpufreq_rwsem))
1058 return 0;
1059
Viresh Kumarfcf80582013-01-29 14:39:08 +00001060#ifdef CONFIG_HOTPLUG_CPU
1061 /* Check if this cpu was hot-unplugged earlier and has siblings */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001062 read_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar1b274292013-08-20 12:08:26 +05301063 list_for_each_entry(tpolicy, &cpufreq_policy_list, policy_list) {
1064 if (cpumask_test_cpu(cpu, tpolicy->related_cpus)) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001065 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301066 ret = cpufreq_add_policy_cpu(tpolicy, cpu, dev);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301067 up_read(&cpufreq_rwsem);
1068 return ret;
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301069 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001070 }
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001071 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +00001072#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001074 /*
1075 * Restore the saved policy when doing light-weight init and fall back
1076 * to the full init if that fails.
1077 */
1078 policy = frozen ? cpufreq_policy_restore(cpu) : NULL;
1079 if (!policy) {
1080 frozen = false;
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301081 policy = cpufreq_policy_alloc();
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001082 if (!policy)
1083 goto nomem_out;
1084 }
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301085
1086 /*
1087 * In the resume path, since we restore a saved policy, the assignment
1088 * to policy->cpu is like an update of the existing policy, rather than
1089 * the creation of a brand new one. So we need to perform this update
1090 * by invoking update_policy_cpu().
1091 */
1092 if (frozen && cpu != policy->cpu)
1093 update_policy_cpu(policy, cpu);
1094 else
1095 policy->cpu = cpu;
1096
Viresh Kumar65922462013-02-07 10:56:03 +05301097 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Rusty Russell835481d2009-01-04 05:18:06 -08001098 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 init_completion(&policy->kobj_unregister);
David Howells65f27f32006-11-22 14:55:48 +00001101 INIT_WORK(&policy->update, handle_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 /* call driver. From then on the cpufreq must be able
1104 * to accept all calls to ->verify and ->setpolicy for this CPU
1105 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001106 ret = cpufreq_driver->init(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001108 pr_debug("initialization failed\n");
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301109 goto err_set_policy_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 }
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001111
Viresh Kumar652ed952014-01-09 20:38:43 +05301112 write_lock_irqsave(&cpufreq_driver_lock, flags);
1113 for_each_cpu(j, policy->cpus)
1114 per_cpu(cpufreq_cpu_data, j) = policy;
1115 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1116
Viresh Kumarda60ce92013-10-03 20:28:30 +05301117 if (cpufreq_driver->get) {
1118 policy->cur = cpufreq_driver->get(policy->cpu);
1119 if (!policy->cur) {
1120 pr_err("%s: ->get() failed\n", __func__);
1121 goto err_get_freq;
1122 }
1123 }
1124
Viresh Kumard3916692013-12-03 11:20:46 +05301125 /*
1126 * Sometimes boot loaders set CPU frequency to a value outside of
1127 * frequency table present with cpufreq core. In such cases CPU might be
1128 * unstable if it has to run on that frequency for long duration of time
1129 * and so its better to set it to a frequency which is specified in
1130 * freq-table. This also makes cpufreq stats inconsistent as
1131 * cpufreq-stats would fail to register because current frequency of CPU
1132 * isn't found in freq-table.
1133 *
1134 * Because we don't want this change to effect boot process badly, we go
1135 * for the next freq which is >= policy->cur ('cur' must be set by now,
1136 * otherwise we will end up setting freq to lowest of the table as 'cur'
1137 * is initialized to zero).
1138 *
1139 * We are passing target-freq as "policy->cur - 1" otherwise
1140 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1141 * equal to target-freq.
1142 */
1143 if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1144 && has_target()) {
1145 /* Are we running at unknown frequency ? */
1146 ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1147 if (ret == -EINVAL) {
1148 /* Warn user and fix it */
1149 pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1150 __func__, policy->cpu, policy->cur);
1151 ret = __cpufreq_driver_target(policy, policy->cur - 1,
1152 CPUFREQ_RELATION_L);
1153
1154 /*
1155 * Reaching here after boot in a few seconds may not
1156 * mean that system will remain stable at "unknown"
1157 * frequency for longer duration. Hence, a BUG_ON().
1158 */
1159 BUG_ON(ret);
1160 pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1161 __func__, policy->cpu, policy->cur);
1162 }
1163 }
1164
Viresh Kumarfcf80582013-01-29 14:39:08 +00001165 /* related cpus should atleast have policy->cpus */
1166 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
1167
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001168 /*
1169 * affected cpus must always be the one, which are online. We aren't
1170 * managing offline cpus here.
1171 */
1172 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1173
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301174 if (!frozen) {
1175 policy->user_policy.min = policy->min;
1176 policy->user_policy.max = policy->max;
1177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Thomas Renningera1531ac2008-07-29 22:32:58 -07001179 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1180 CPUFREQ_START, policy);
1181
Viresh Kumarfcf80582013-01-29 14:39:08 +00001182#ifdef CONFIG_HOTPLUG_CPU
1183 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
1184 if (gov) {
1185 policy->governor = gov;
1186 pr_debug("Restoring governor %s for cpu %d\n",
1187 policy->governor->name, cpu);
Thomas Renninger4bfa0422009-07-24 15:25:03 +02001188 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001189#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
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 }
1209
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001210 kobject_uevent(&policy->kobj, KOBJ_ADD);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301211 up_read(&cpufreq_rwsem);
1212
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001213 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 return 0;
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217err_out_unregister:
Viresh Kumar652ed952014-01-09 20:38:43 +05301218err_get_freq:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001219 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar474deff2013-08-20 12:08:25 +05301220 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -07001221 per_cpu(cpufreq_cpu_data, j) = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001222 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Viresh Kumarda60ce92013-10-03 20:28:30 +05301224 if (cpufreq_driver->exit)
1225 cpufreq_driver->exit(policy);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301226err_set_policy_cpu:
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001227 if (frozen) {
1228 /* Do not leave stale fallback data behind. */
1229 per_cpu(cpufreq_cpu_data_fallback, cpu) = NULL;
Viresh Kumar42f921a2013-12-20 21:26:02 +05301230 cpufreq_policy_put_kobj(policy);
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001231 }
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301232 cpufreq_policy_free(policy);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234nomem_out:
Viresh Kumar6eed9402013-08-06 22:53:11 +05301235 up_read(&cpufreq_rwsem);
1236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return ret;
1238}
1239
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301240/**
1241 * cpufreq_add_dev - add a CPU device
1242 *
1243 * Adds the cpufreq interface for a CPU device.
1244 *
1245 * The Oracle says: try running cpufreq registration/unregistration concurrently
1246 * with with cpu hotplugging and all hell will break loose. Tried to clean this
1247 * mess up, but more thorough testing is needed. - Mathieu
1248 */
1249static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1250{
1251 return __cpufreq_add_dev(dev, sif, false);
1252}
1253
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301254static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy *policy,
Viresh Kumar42f921a2013-12-20 21:26:02 +05301255 unsigned int old_cpu)
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301256{
1257 struct device *cpu_dev;
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301258 int ret;
1259
1260 /* first sibling now owns the new sysfs dir */
Viresh Kumar9c8f1ee2013-09-12 17:06:33 +05301261 cpu_dev = get_cpu_device(cpumask_any_but(policy->cpus, old_cpu));
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301262
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301263 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301264 ret = kobject_move(&policy->kobj, &cpu_dev->kobj);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301265 if (ret) {
1266 pr_err("%s: Failed to move kobj: %d", __func__, ret);
1267
viresh kumarad7722d2013-10-18 19:10:15 +05301268 down_write(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301269 cpumask_set_cpu(old_cpu, policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301270 up_write(&policy->rwsem);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301271
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301272 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301273 "cpufreq");
1274
1275 return -EINVAL;
1276 }
1277
1278 return cpu_dev->id;
1279}
1280
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301281static int __cpufreq_remove_dev_prepare(struct device *dev,
1282 struct subsys_interface *sif,
1283 bool frozen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301285 unsigned int cpu = dev->id, cpus;
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301286 int new_cpu, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 unsigned long flags;
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301288 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001290 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001292 write_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301294 policy = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301296 /* Save the policy somewhere when doing a light-weight tear-down */
1297 if (frozen)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301298 per_cpu(cpufreq_cpu_data_fallback, cpu) = policy;
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301299
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001300 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301302 if (!policy) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001303 pr_debug("%s: No cpu_data found\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301307 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301308 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1309 if (ret) {
1310 pr_err("%s: Failed to stop governor\n", __func__);
1311 return ret;
1312 }
1313 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001314
Jacob Shin27ecddc2011-04-27 13:32:11 -05001315#ifdef CONFIG_HOTPLUG_CPU
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#endif
1320
viresh kumarad7722d2013-10-18 19:10:15 +05301321 down_read(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301322 cpus = cpumask_weight(policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301323 up_read(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Srivatsa S. Bhat61173f22013-09-12 01:43:25 +05301325 if (cpu != policy->cpu) {
viresh kumar6964d912014-02-17 14:52:11 +05301326 sysfs_remove_link(&dev->kobj, "cpufreq");
Viresh Kumar73bf0fc2013-02-05 22:21:14 +01001327 } else if (cpus > 1) {
Viresh Kumar42f921a2013-12-20 21:26:02 +05301328 new_cpu = cpufreq_nominate_new_policy_cpu(policy, cpu);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301329 if (new_cpu >= 0) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301330 update_policy_cpu(policy, new_cpu);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301331
1332 if (!frozen) {
Viresh Kumar75949c92013-10-02 14:13:13 +05301333 pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
1334 __func__, new_cpu, cpu);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301335 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001336 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001337 }
Venki Pallipadiec282972007-03-26 12:03:19 -07001338
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301339 return 0;
1340}
1341
1342static int __cpufreq_remove_dev_finish(struct device *dev,
1343 struct subsys_interface *sif,
1344 bool frozen)
1345{
1346 unsigned int cpu = dev->id, cpus;
1347 int ret;
1348 unsigned long flags;
1349 struct cpufreq_policy *policy;
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301350
1351 read_lock_irqsave(&cpufreq_driver_lock, flags);
1352 policy = per_cpu(cpufreq_cpu_data, cpu);
1353 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1354
1355 if (!policy) {
1356 pr_debug("%s: No cpu_data found\n", __func__);
1357 return -EINVAL;
1358 }
1359
viresh kumarad7722d2013-10-18 19:10:15 +05301360 down_write(&policy->rwsem);
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301361 cpus = cpumask_weight(policy->cpus);
Viresh Kumar9c8f1ee2013-09-12 17:06:33 +05301362
1363 if (cpus > 1)
1364 cpumask_clear_cpu(cpu, policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301365 up_write(&policy->rwsem);
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301366
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001367 /* If cpu is last user of policy, free policy */
1368 if (cpus == 1) {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301369 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301370 ret = __cpufreq_governor(policy,
1371 CPUFREQ_GOV_POLICY_EXIT);
1372 if (ret) {
1373 pr_err("%s: Failed to exit governor\n",
1374 __func__);
1375 return ret;
1376 }
Viresh Kumaredab2fb2013-08-20 12:08:22 +05301377 }
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001378
Viresh Kumar42f921a2013-12-20 21:26:02 +05301379 if (!frozen)
1380 cpufreq_policy_put_kobj(policy);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301381
1382 /*
1383 * Perform the ->exit() even during light-weight tear-down,
1384 * since this is a core component, and is essential for the
1385 * subsequent light-weight ->init() to succeed.
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001386 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001387 if (cpufreq_driver->exit)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301388 cpufreq_driver->exit(policy);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001389
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301390 /* Remove policy from list of active policies */
1391 write_lock_irqsave(&cpufreq_driver_lock, flags);
1392 list_del(&policy->policy_list);
1393 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1394
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301395 if (!frozen)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301396 cpufreq_policy_free(policy);
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001397 } else {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301398 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301399 if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
1400 (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
1401 pr_err("%s: Failed to start governor\n",
1402 __func__);
1403 return ret;
1404 }
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001405 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Viresh Kumar474deff2013-08-20 12:08:25 +05301408 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return 0;
1410}
1411
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301412/**
Viresh Kumar27a862e2013-10-02 14:13:14 +05301413 * cpufreq_remove_dev - remove a CPU device
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301414 *
1415 * Removes the cpufreq interface for a CPU device.
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301416 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001417static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001418{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001419 unsigned int cpu = dev->id;
Viresh Kumar27a862e2013-10-02 14:13:14 +05301420 int ret;
Venki Pallipadiec282972007-03-26 12:03:19 -07001421
1422 if (cpu_is_offline(cpu))
1423 return 0;
1424
Viresh Kumar27a862e2013-10-02 14:13:14 +05301425 ret = __cpufreq_remove_dev_prepare(dev, sif, false);
1426
1427 if (!ret)
1428 ret = __cpufreq_remove_dev_finish(dev, sif, false);
1429
1430 return ret;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001431}
1432
David Howells65f27f32006-11-22 14:55:48 +00001433static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
David Howells65f27f32006-11-22 14:55:48 +00001435 struct cpufreq_policy *policy =
1436 container_of(work, struct cpufreq_policy, update);
1437 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001438 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 cpufreq_update_policy(cpu);
1440}
1441
1442/**
Viresh Kumarbb176f72013-06-19 14:19:33 +05301443 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1444 * in deep trouble.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 * @cpu: cpu number
1446 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1447 * @new_freq: CPU frequency the CPU actually runs at
1448 *
Dave Jones29464f22009-01-18 01:37:11 -05001449 * We adjust to current frequency first, and need to clean up later.
1450 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301452static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1453 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454{
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301455 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301457 unsigned long flags;
1458
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001459 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 freqs.old = old_freq;
1463 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301464
1465 read_lock_irqsave(&cpufreq_driver_lock, flags);
1466 policy = per_cpu(cpufreq_cpu_data, cpu);
1467 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1468
1469 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1470 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471}
1472
Dave Jones32ee8c32006-02-28 00:43:23 -05001473/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301474 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001475 * @cpu: CPU number
1476 *
1477 * This is the last known freq, without actually getting it from the driver.
1478 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1479 */
1480unsigned int cpufreq_quick_get(unsigned int cpu)
1481{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001482 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301483 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001484
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001485 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1486 return cpufreq_driver->get(cpu);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001487
1488 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001489 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301490 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001491 cpufreq_cpu_put(policy);
1492 }
1493
Dave Jones4d34a672008-02-07 16:33:49 -05001494 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001495}
1496EXPORT_SYMBOL(cpufreq_quick_get);
1497
Jesse Barnes3d737102011-06-28 10:59:12 -07001498/**
1499 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1500 * @cpu: CPU number
1501 *
1502 * Just return the max possible frequency for a given CPU.
1503 */
1504unsigned int cpufreq_quick_get_max(unsigned int cpu)
1505{
1506 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1507 unsigned int ret_freq = 0;
1508
1509 if (policy) {
1510 ret_freq = policy->max;
1511 cpufreq_cpu_put(policy);
1512 }
1513
1514 return ret_freq;
1515}
1516EXPORT_SYMBOL(cpufreq_quick_get_max);
1517
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001518static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001520 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301521 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001523 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001524 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001526 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301528 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001529 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301530 /* verify no discrepancy between actual and
1531 saved value exists */
1532 if (unlikely(ret_freq != policy->cur)) {
1533 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 schedule_work(&policy->update);
1535 }
1536 }
1537
Dave Jones4d34a672008-02-07 16:33:49 -05001538 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001539}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001541/**
1542 * cpufreq_get - get the current CPU frequency (in kHz)
1543 * @cpu: CPU number
1544 *
1545 * Get the CPU current (static) CPU frequency
1546 */
1547unsigned int cpufreq_get(unsigned int cpu)
1548{
viresh kumarad7722d2013-10-18 19:10:15 +05301549 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001550 unsigned int ret_freq = 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001551
Viresh Kumar26ca8692013-09-20 22:37:31 +05301552 if (cpufreq_disabled() || !cpufreq_driver)
1553 return -ENOENT;
1554
viresh kumarad7722d2013-10-18 19:10:15 +05301555 BUG_ON(!policy);
1556
Viresh Kumar6eed9402013-08-06 22:53:11 +05301557 if (!down_read_trylock(&cpufreq_rwsem))
1558 return 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001559
viresh kumarad7722d2013-10-18 19:10:15 +05301560 down_read(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001561
1562 ret_freq = __cpufreq_get(cpu);
1563
viresh kumarad7722d2013-10-18 19:10:15 +05301564 up_read(&policy->rwsem);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301565 up_read(&cpufreq_rwsem);
1566
Dave Jones4d34a672008-02-07 16:33:49 -05001567 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568}
1569EXPORT_SYMBOL(cpufreq_get);
1570
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001571static struct subsys_interface cpufreq_interface = {
1572 .name = "cpufreq",
1573 .subsys = &cpu_subsys,
1574 .add_dev = cpufreq_add_dev,
1575 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001576};
1577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001579 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1580 *
1581 * This function is only executed for the boot processor. The other CPUs
1582 * have been put offline by means of CPU hotplug.
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001583 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001584static int cpufreq_bp_suspend(void)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001585{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301586 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001587
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001588 int cpu = smp_processor_id();
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301589 struct cpufreq_policy *policy;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001590
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001591 pr_debug("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001592
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001593 /* If there's no policy for the boot CPU, we have nothing to do. */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301594 policy = cpufreq_cpu_get(cpu);
1595 if (!policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001596 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001597
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001598 if (cpufreq_driver->suspend) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301599 ret = cpufreq_driver->suspend(policy);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001600 if (ret)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001601 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301602 "step on CPU %u\n", policy->cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001603 }
1604
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301605 cpufreq_cpu_put(policy);
Dave Jonesc9060492008-02-07 16:32:18 -05001606 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001607}
1608
1609/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001610 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 *
1612 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001613 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1614 * restored. It will verify that the current freq is in sync with
1615 * what we believe it to be. This is a bit later than when it
1616 * should be, but nonethteless it's better than calling
1617 * cpufreq_driver->get() here which might re-enable interrupts...
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001618 *
1619 * This function is only executed for the boot CPU. The other CPUs have not
1620 * been turned on yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001622static void cpufreq_bp_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301624 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001625
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001626 int cpu = smp_processor_id();
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301627 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001629 pr_debug("resuming cpu %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001631 /* If there's no policy for the boot CPU, we have nothing to do. */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301632 policy = cpufreq_cpu_get(cpu);
1633 if (!policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001634 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001636 if (cpufreq_driver->resume) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301637 ret = cpufreq_driver->resume(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 if (ret) {
1639 printk(KERN_ERR "cpufreq: resume failed in ->resume "
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301640 "step on CPU %u\n", policy->cpu);
Dave Jonesc9060492008-02-07 16:32:18 -05001641 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 }
1643 }
1644
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301645 schedule_work(&policy->update);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001646
Dave Jonesc9060492008-02-07 16:32:18 -05001647fail:
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301648 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649}
1650
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001651static struct syscore_ops cpufreq_syscore_ops = {
1652 .suspend = cpufreq_bp_suspend,
1653 .resume = cpufreq_bp_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654};
1655
Borislav Petkov9d950462013-01-20 10:24:28 +00001656/**
1657 * cpufreq_get_current_driver - return current driver's name
1658 *
1659 * Return the name string of the currently loaded cpufreq driver
1660 * or NULL, if none.
1661 */
1662const char *cpufreq_get_current_driver(void)
1663{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001664 if (cpufreq_driver)
1665 return cpufreq_driver->name;
1666
1667 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001668}
1669EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
1671/*********************************************************************
1672 * NOTIFIER LISTS INTERFACE *
1673 *********************************************************************/
1674
1675/**
1676 * cpufreq_register_notifier - register a driver with cpufreq
1677 * @nb: notifier function to register
1678 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1679 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001680 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 * are notified about clock rate changes (once before and once after
1682 * the transition), or a list of drivers that are notified about
1683 * changes in cpufreq policy.
1684 *
1685 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001686 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 */
1688int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1689{
1690 int ret;
1691
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001692 if (cpufreq_disabled())
1693 return -EINVAL;
1694
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001695 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 switch (list) {
1698 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001699 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001700 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 break;
1702 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001703 ret = blocking_notifier_chain_register(
1704 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 break;
1706 default:
1707 ret = -EINVAL;
1708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
1710 return ret;
1711}
1712EXPORT_SYMBOL(cpufreq_register_notifier);
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714/**
1715 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1716 * @nb: notifier block to be unregistered
Viresh Kumarbb176f72013-06-19 14:19:33 +05301717 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 *
1719 * Remove a driver from the CPU frequency notifier list.
1720 *
1721 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001722 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 */
1724int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1725{
1726 int ret;
1727
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001728 if (cpufreq_disabled())
1729 return -EINVAL;
1730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 switch (list) {
1732 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001733 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001734 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 break;
1736 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001737 ret = blocking_notifier_chain_unregister(
1738 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 break;
1740 default:
1741 ret = -EINVAL;
1742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
1744 return ret;
1745}
1746EXPORT_SYMBOL(cpufreq_unregister_notifier);
1747
1748
1749/*********************************************************************
1750 * GOVERNORS *
1751 *********************************************************************/
1752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753int __cpufreq_driver_target(struct cpufreq_policy *policy,
1754 unsigned int target_freq,
1755 unsigned int relation)
1756{
1757 int retval = -EINVAL;
Viresh Kumar72499242012-10-31 01:28:21 +01001758 unsigned int old_target_freq = target_freq;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001759
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001760 if (cpufreq_disabled())
1761 return -ENODEV;
1762
Viresh Kumar72499242012-10-31 01:28:21 +01001763 /* Make sure that target_freq is within supported range */
1764 if (target_freq > policy->max)
1765 target_freq = policy->max;
1766 if (target_freq < policy->min)
1767 target_freq = policy->min;
1768
1769 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1770 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001771
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301772 /*
1773 * This might look like a redundant call as we are checking it again
1774 * after finding index. But it is left intentionally for cases where
1775 * exactly same freq is called again and so we can save on few function
1776 * calls.
1777 */
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001778 if (target_freq == policy->cur)
1779 return 0;
1780
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001781 if (cpufreq_driver->target)
1782 retval = cpufreq_driver->target(policy, target_freq, relation);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301783 else if (cpufreq_driver->target_index) {
1784 struct cpufreq_frequency_table *freq_table;
Viresh Kumard4019f02013-08-14 19:38:24 +05301785 struct cpufreq_freqs freqs;
1786 bool notify;
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301787 int index;
Ashok Raj90d45d12005-11-08 21:34:24 -08001788
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301789 freq_table = cpufreq_frequency_get_table(policy->cpu);
1790 if (unlikely(!freq_table)) {
1791 pr_err("%s: Unable to find freq_table\n", __func__);
1792 goto out;
1793 }
1794
1795 retval = cpufreq_frequency_table_target(policy, freq_table,
1796 target_freq, relation, &index);
1797 if (unlikely(retval)) {
1798 pr_err("%s: Unable to find matching freq\n", __func__);
1799 goto out;
1800 }
1801
Viresh Kumard4019f02013-08-14 19:38:24 +05301802 if (freq_table[index].frequency == policy->cur) {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301803 retval = 0;
Viresh Kumard4019f02013-08-14 19:38:24 +05301804 goto out;
1805 }
1806
1807 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
1808
1809 if (notify) {
1810 freqs.old = policy->cur;
1811 freqs.new = freq_table[index].frequency;
1812 freqs.flags = 0;
1813
1814 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1815 __func__, policy->cpu, freqs.old,
1816 freqs.new);
1817
1818 cpufreq_notify_transition(policy, &freqs,
1819 CPUFREQ_PRECHANGE);
1820 }
1821
1822 retval = cpufreq_driver->target_index(policy, index);
1823 if (retval)
1824 pr_err("%s: Failed to change cpu frequency: %d\n",
1825 __func__, retval);
1826
Viresh Kumarab1b1c42013-12-02 11:04:13 +05301827 if (notify)
1828 cpufreq_notify_post_transition(policy, &freqs, retval);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301829 }
1830
1831out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 return retval;
1833}
1834EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836int cpufreq_driver_target(struct cpufreq_policy *policy,
1837 unsigned int target_freq,
1838 unsigned int relation)
1839{
Julia Lawallf1829e42008-07-25 22:44:53 +02001840 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
viresh kumarad7722d2013-10-18 19:10:15 +05301842 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
1844 ret = __cpufreq_driver_target(policy, target_freq, relation);
1845
viresh kumarad7722d2013-10-18 19:10:15 +05301846 up_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 return ret;
1849}
1850EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1851
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001852/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001853 * when "event" is CPUFREQ_GOV_LIMITS
1854 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301856static int __cpufreq_governor(struct cpufreq_policy *policy,
1857 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
Dave Jonescc993ca2005-07-28 09:43:56 -07001859 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001860
1861 /* Only must be defined when default governor is known to have latency
1862 restrictions, like e.g. conservative or ondemand.
1863 That this is the case is already ensured in Kconfig
1864 */
1865#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1866 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1867#else
1868 struct cpufreq_governor *gov = NULL;
1869#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001870
1871 if (policy->governor->max_transition_latency &&
1872 policy->cpuinfo.transition_latency >
1873 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07001874 if (!gov)
1875 return -EINVAL;
1876 else {
1877 printk(KERN_WARNING "%s governor failed, too long"
1878 " transition latency of HW, fallback"
1879 " to %s governor\n",
1880 policy->governor->name,
1881 gov->name);
1882 policy->governor = gov;
1883 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Viresh Kumarfe492f32013-08-06 22:53:10 +05301886 if (event == CPUFREQ_GOV_POLICY_INIT)
1887 if (!try_module_get(policy->governor->owner))
1888 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001890 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301891 policy->cpu, event);
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001892
1893 mutex_lock(&cpufreq_governor_lock);
Srivatsa S. Bhat56d07db2013-09-07 01:23:55 +05301894 if ((policy->governor_enabled && event == CPUFREQ_GOV_START)
Viresh Kumarf73d3932013-08-31 17:53:40 +05301895 || (!policy->governor_enabled
1896 && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) {
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001897 mutex_unlock(&cpufreq_governor_lock);
1898 return -EBUSY;
1899 }
1900
1901 if (event == CPUFREQ_GOV_STOP)
1902 policy->governor_enabled = false;
1903 else if (event == CPUFREQ_GOV_START)
1904 policy->governor_enabled = true;
1905
1906 mutex_unlock(&cpufreq_governor_lock);
1907
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 ret = policy->governor->governor(policy, event);
1909
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001910 if (!ret) {
1911 if (event == CPUFREQ_GOV_POLICY_INIT)
1912 policy->governor->initialized++;
1913 else if (event == CPUFREQ_GOV_POLICY_EXIT)
1914 policy->governor->initialized--;
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001915 } else {
1916 /* Restore original values */
1917 mutex_lock(&cpufreq_governor_lock);
1918 if (event == CPUFREQ_GOV_STOP)
1919 policy->governor_enabled = true;
1920 else if (event == CPUFREQ_GOV_START)
1921 policy->governor_enabled = false;
1922 mutex_unlock(&cpufreq_governor_lock);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001923 }
Viresh Kumarb3940582013-02-01 05:42:58 +00001924
Viresh Kumarfe492f32013-08-06 22:53:10 +05301925 if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
1926 ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 module_put(policy->governor->owner);
1928
1929 return ret;
1930}
1931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932int cpufreq_register_governor(struct cpufreq_governor *governor)
1933{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001934 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
1936 if (!governor)
1937 return -EINVAL;
1938
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001939 if (cpufreq_disabled())
1940 return -ENODEV;
1941
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001942 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001943
Viresh Kumarb3940582013-02-01 05:42:58 +00001944 governor->initialized = 0;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001945 err = -EBUSY;
1946 if (__find_governor(governor->name) == NULL) {
1947 err = 0;
1948 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Dave Jones32ee8c32006-02-28 00:43:23 -05001951 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001952 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953}
1954EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1955
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1957{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001958#ifdef CONFIG_HOTPLUG_CPU
1959 int cpu;
1960#endif
1961
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 if (!governor)
1963 return;
1964
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001965 if (cpufreq_disabled())
1966 return;
1967
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001968#ifdef CONFIG_HOTPLUG_CPU
1969 for_each_present_cpu(cpu) {
1970 if (cpu_online(cpu))
1971 continue;
1972 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1973 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1974 }
1975#endif
1976
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001977 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001979 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 return;
1981}
1982EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1983
1984
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985/*********************************************************************
1986 * POLICY INTERFACE *
1987 *********************************************************************/
1988
1989/**
1990 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05001991 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1992 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 *
1994 * Reads the current cpufreq policy.
1995 */
1996int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1997{
1998 struct cpufreq_policy *cpu_policy;
1999 if (!policy)
2000 return -EINVAL;
2001
2002 cpu_policy = cpufreq_cpu_get(cpu);
2003 if (!cpu_policy)
2004 return -EINVAL;
2005
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302006 memcpy(policy, cpu_policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
2008 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 return 0;
2010}
2011EXPORT_SYMBOL(cpufreq_get_policy);
2012
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002013/*
Viresh Kumar037ce832013-10-02 14:13:16 +05302014 * policy : current policy.
2015 * new_policy: policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002016 */
Viresh Kumar037ce832013-10-02 14:13:16 +05302017static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302018 struct cpufreq_policy *new_policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019{
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002020 struct cpufreq_governor *old_gov;
2021 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302023 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", new_policy->cpu,
2024 new_policy->min, new_policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302026 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002028 if (new_policy->min > policy->max || new_policy->max < policy->min)
2029 return -EINVAL;
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02002030
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 /* verify the cpu speed can be set within this limit */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302032 ret = cpufreq_driver->verify(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002034 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08002037 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302038 CPUFREQ_ADJUST, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
2040 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08002041 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302042 CPUFREQ_INCOMPATIBLE, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Viresh Kumarbb176f72013-06-19 14:19:33 +05302044 /*
2045 * verify the cpu speed can be set within this limit, which might be
2046 * different to the first one
2047 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302048 ret = cpufreq_driver->verify(new_policy);
Alan Sterne041c682006-03-27 01:16:30 -08002049 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002050 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
2052 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08002053 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302054 CPUFREQ_NOTIFY, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302056 policy->min = new_policy->min;
2057 policy->max = new_policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002059 pr_debug("new min and max freqs are %u - %u kHz\n",
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302060 policy->min, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002062 if (cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302063 policy->policy = new_policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002064 pr_debug("setting range\n");
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002065 return cpufreq_driver->setpolicy(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 }
2067
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002068 if (new_policy->governor == policy->governor)
2069 goto out;
2070
2071 pr_debug("governor switch\n");
2072
2073 /* save old, working values */
2074 old_gov = policy->governor;
2075 /* end old governor */
2076 if (old_gov) {
2077 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
2078 up_write(&policy->rwsem);
2079 __cpufreq_governor(policy,CPUFREQ_GOV_POLICY_EXIT);
2080 down_write(&policy->rwsem);
2081 }
2082
2083 /* start new governor */
2084 policy->governor = new_policy->governor;
2085 if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
2086 if (!__cpufreq_governor(policy, CPUFREQ_GOV_START))
2087 goto out;
2088
2089 up_write(&policy->rwsem);
2090 __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
2091 down_write(&policy->rwsem);
2092 }
2093
2094 /* new governor failed, so re-start old one */
2095 pr_debug("starting governor %s failed\n", policy->governor->name);
2096 if (old_gov) {
2097 policy->governor = old_gov;
2098 __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT);
2099 __cpufreq_governor(policy, CPUFREQ_GOV_START);
2100 }
2101
2102 return -EINVAL;
2103
2104 out:
2105 pr_debug("governor: change or update limits\n");
2106 return __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107}
2108
2109/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
2111 * @cpu: CPU which shall be re-evaluated
2112 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002113 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 * at different times.
2115 */
2116int cpufreq_update_policy(unsigned int cpu)
2117{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302118 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2119 struct cpufreq_policy new_policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02002120 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302122 if (!policy) {
Julia Lawallf1829e42008-07-25 22:44:53 +02002123 ret = -ENODEV;
2124 goto no_policy;
2125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
viresh kumarad7722d2013-10-18 19:10:15 +05302127 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002129 pr_debug("updating policy for CPU %u\n", cpu);
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302130 memcpy(&new_policy, policy, sizeof(*policy));
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302131 new_policy.min = policy->user_policy.min;
2132 new_policy.max = policy->user_policy.max;
2133 new_policy.policy = policy->user_policy.policy;
2134 new_policy.governor = policy->user_policy.governor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Viresh Kumarbb176f72013-06-19 14:19:33 +05302136 /*
2137 * BIOS might change freq behind our back
2138 * -> ask driver for current freq and notify governors about a change
2139 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002140 if (cpufreq_driver->get) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302141 new_policy.cur = cpufreq_driver->get(cpu);
2142 if (!policy->cur) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002143 pr_debug("Driver did not initialize current freq");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302144 policy->cur = new_policy.cur;
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002145 } else {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302146 if (policy->cur != new_policy.cur && has_target())
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302147 cpufreq_out_of_sync(cpu, policy->cur,
2148 new_policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002149 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01002150 }
2151
Viresh Kumar037ce832013-10-02 14:13:16 +05302152 ret = cpufreq_set_policy(policy, &new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
viresh kumarad7722d2013-10-18 19:10:15 +05302154 up_write(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002155
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302156 cpufreq_cpu_put(policy);
Julia Lawallf1829e42008-07-25 22:44:53 +02002157no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 return ret;
2159}
2160EXPORT_SYMBOL(cpufreq_update_policy);
2161
Paul Gortmaker27609842013-06-19 13:54:04 -04002162static int cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002163 unsigned long action, void *hcpu)
2164{
2165 unsigned int cpu = (unsigned long)hcpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002166 struct device *dev;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302167 bool frozen = false;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002168
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002169 dev = get_cpu_device(cpu);
2170 if (dev) {
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302171
Rafael J. Wysockid4faadd2013-12-08 01:23:58 +01002172 if (action & CPU_TASKS_FROZEN)
2173 frozen = true;
2174
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302175 switch (action & ~CPU_TASKS_FROZEN) {
Ashok Rajc32b6b82005-10-30 14:59:54 -08002176 case CPU_ONLINE:
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302177 __cpufreq_add_dev(dev, NULL, frozen);
Srivatsa S. Bhat23d328992013-07-30 04:23:56 +05302178 cpufreq_update_policy(cpu);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002179 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302180
Ashok Rajc32b6b82005-10-30 14:59:54 -08002181 case CPU_DOWN_PREPARE:
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05302182 __cpufreq_remove_dev_prepare(dev, NULL, frozen);
Srivatsa S. Bhat1aee40a2013-09-07 01:23:27 +05302183 break;
2184
2185 case CPU_POST_DEAD:
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05302186 __cpufreq_remove_dev_finish(dev, NULL, frozen);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002187 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302188
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002189 case CPU_DOWN_FAILED:
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302190 __cpufreq_add_dev(dev, NULL, frozen);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002191 break;
2192 }
2193 }
2194 return NOTIFY_OK;
2195}
2196
Neal Buckendahl9c36f742010-06-22 22:02:44 -05002197static struct notifier_block __refdata cpufreq_cpu_notifier = {
Viresh Kumarbb176f72013-06-19 14:19:33 +05302198 .notifier_call = cpufreq_cpu_callback,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002199};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
2201/*********************************************************************
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002202 * BOOST *
2203 *********************************************************************/
2204static int cpufreq_boost_set_sw(int state)
2205{
2206 struct cpufreq_frequency_table *freq_table;
2207 struct cpufreq_policy *policy;
2208 int ret = -EINVAL;
2209
2210 list_for_each_entry(policy, &cpufreq_policy_list, policy_list) {
2211 freq_table = cpufreq_frequency_get_table(policy->cpu);
2212 if (freq_table) {
2213 ret = cpufreq_frequency_table_cpuinfo(policy,
2214 freq_table);
2215 if (ret) {
2216 pr_err("%s: Policy frequency update failed\n",
2217 __func__);
2218 break;
2219 }
2220 policy->user_policy.max = policy->max;
2221 __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
2222 }
2223 }
2224
2225 return ret;
2226}
2227
2228int cpufreq_boost_trigger_state(int state)
2229{
2230 unsigned long flags;
2231 int ret = 0;
2232
2233 if (cpufreq_driver->boost_enabled == state)
2234 return 0;
2235
2236 write_lock_irqsave(&cpufreq_driver_lock, flags);
2237 cpufreq_driver->boost_enabled = state;
2238 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2239
2240 ret = cpufreq_driver->set_boost(state);
2241 if (ret) {
2242 write_lock_irqsave(&cpufreq_driver_lock, flags);
2243 cpufreq_driver->boost_enabled = !state;
2244 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2245
2246 pr_err("%s: Cannot %s BOOST\n", __func__,
2247 state ? "enable" : "disable");
2248 }
2249
2250 return ret;
2251}
2252
2253int cpufreq_boost_supported(void)
2254{
2255 if (likely(cpufreq_driver))
2256 return cpufreq_driver->boost_supported;
2257
2258 return 0;
2259}
2260EXPORT_SYMBOL_GPL(cpufreq_boost_supported);
2261
2262int cpufreq_boost_enabled(void)
2263{
2264 return cpufreq_driver->boost_enabled;
2265}
2266EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2267
2268/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2270 *********************************************************************/
2271
2272/**
2273 * cpufreq_register_driver - register a CPU Frequency driver
2274 * @driver_data: A struct cpufreq_driver containing the values#
2275 * submitted by the CPU Frequency driver.
2276 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302277 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05002279 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 *
2281 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002282int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283{
2284 unsigned long flags;
2285 int ret;
2286
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002287 if (cpufreq_disabled())
2288 return -ENODEV;
2289
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 if (!driver_data || !driver_data->verify || !driver_data->init ||
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302291 !(driver_data->setpolicy || driver_data->target_index ||
2292 driver_data->target))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 return -EINVAL;
2294
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002295 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
2297 if (driver_data->setpolicy)
2298 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2299
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002300 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002301 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002302 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Yinghai Lu4dea58062013-09-18 21:05:20 -07002303 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002305 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002306 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002308 if (cpufreq_boost_supported()) {
2309 /*
2310 * Check if driver provides function to enable boost -
2311 * if not, use cpufreq_boost_set_sw as default
2312 */
2313 if (!cpufreq_driver->set_boost)
2314 cpufreq_driver->set_boost = cpufreq_boost_set_sw;
2315
2316 ret = cpufreq_sysfs_create_file(&boost.attr);
2317 if (ret) {
2318 pr_err("%s: cannot register global BOOST sysfs file\n",
2319 __func__);
2320 goto err_null_driver;
2321 }
2322 }
2323
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002324 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002325 if (ret)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002326 goto err_boost_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002328 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 int i;
2330 ret = -ENODEV;
2331
2332 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07002333 for (i = 0; i < nr_cpu_ids; i++)
2334 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07002336 break;
2337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
2339 /* if all ->init() calls failed, unregister */
2340 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002341 pr_debug("no CPU initialized for driver %s\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05302342 driver_data->name);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002343 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 }
2345 }
2346
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002347 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002348 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002350 return 0;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002351err_if_unreg:
2352 subsys_interface_unregister(&cpufreq_interface);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002353err_boost_unreg:
2354 if (cpufreq_boost_supported())
2355 cpufreq_sysfs_remove_file(&boost.attr);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002356err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002357 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002358 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002359 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05002360 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361}
2362EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2363
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364/**
2365 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2366 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302367 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 * the right to do so, i.e. if you have succeeded in initialising before!
2369 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2370 * currently not initialised.
2371 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002372int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373{
2374 unsigned long flags;
2375
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002376 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002379 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002381 subsys_interface_unregister(&cpufreq_interface);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002382 if (cpufreq_boost_supported())
2383 cpufreq_sysfs_remove_file(&boost.attr);
2384
Chandra Seetharaman65edc682006-06-27 02:54:08 -07002385 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
Viresh Kumar6eed9402013-08-06 22:53:11 +05302387 down_write(&cpufreq_rwsem);
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002388 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302389
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002390 cpufreq_driver = NULL;
Viresh Kumar6eed9402013-08-06 22:53:11 +05302391
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002392 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302393 up_write(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
2395 return 0;
2396}
2397EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002398
2399static int __init cpufreq_core_init(void)
2400{
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002401 if (cpufreq_disabled())
2402 return -ENODEV;
2403
Viresh Kumar2361be22013-05-17 16:09:09 +05302404 cpufreq_global_kobject = kobject_create();
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002405 BUG_ON(!cpufreq_global_kobject);
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01002406 register_syscore_ops(&cpufreq_syscore_ops);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002407
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002408 return 0;
2409}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002410core_initcall(cpufreq_core_init);