blob: cf485d9289035fe58ed9c84223ccd9f412a4b6cd [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 Kumar5a7e56a2014-03-04 11:44:00 +08001112 /* related cpus should atleast have policy->cpus */
1113 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
1114
1115 /*
1116 * affected cpus must always be the one, which are online. We aren't
1117 * managing offline cpus here.
1118 */
1119 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1120
1121 if (!frozen) {
1122 policy->user_policy.min = policy->min;
1123 policy->user_policy.max = policy->max;
1124 }
1125
Viresh Kumar4e97b632014-03-04 11:44:01 +08001126 down_write(&policy->rwsem);
Viresh Kumar652ed952014-01-09 20:38:43 +05301127 write_lock_irqsave(&cpufreq_driver_lock, flags);
1128 for_each_cpu(j, policy->cpus)
1129 per_cpu(cpufreq_cpu_data, j) = policy;
1130 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1131
Viresh Kumarda60ce92013-10-03 20:28:30 +05301132 if (cpufreq_driver->get) {
1133 policy->cur = cpufreq_driver->get(policy->cpu);
1134 if (!policy->cur) {
1135 pr_err("%s: ->get() failed\n", __func__);
1136 goto err_get_freq;
1137 }
1138 }
1139
Viresh Kumard3916692013-12-03 11:20:46 +05301140 /*
1141 * Sometimes boot loaders set CPU frequency to a value outside of
1142 * frequency table present with cpufreq core. In such cases CPU might be
1143 * unstable if it has to run on that frequency for long duration of time
1144 * and so its better to set it to a frequency which is specified in
1145 * freq-table. This also makes cpufreq stats inconsistent as
1146 * cpufreq-stats would fail to register because current frequency of CPU
1147 * isn't found in freq-table.
1148 *
1149 * Because we don't want this change to effect boot process badly, we go
1150 * for the next freq which is >= policy->cur ('cur' must be set by now,
1151 * otherwise we will end up setting freq to lowest of the table as 'cur'
1152 * is initialized to zero).
1153 *
1154 * We are passing target-freq as "policy->cur - 1" otherwise
1155 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1156 * equal to target-freq.
1157 */
1158 if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1159 && has_target()) {
1160 /* Are we running at unknown frequency ? */
1161 ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1162 if (ret == -EINVAL) {
1163 /* Warn user and fix it */
1164 pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1165 __func__, policy->cpu, policy->cur);
1166 ret = __cpufreq_driver_target(policy, policy->cur - 1,
1167 CPUFREQ_RELATION_L);
1168
1169 /*
1170 * Reaching here after boot in a few seconds may not
1171 * mean that system will remain stable at "unknown"
1172 * frequency for longer duration. Hence, a BUG_ON().
1173 */
1174 BUG_ON(ret);
1175 pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1176 __func__, policy->cpu, policy->cur);
1177 }
1178 }
1179
Thomas Renningera1531ac2008-07-29 22:32:58 -07001180 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1181 CPUFREQ_START, policy);
1182
Viresh Kumarfcf80582013-01-29 14:39:08 +00001183#ifdef CONFIG_HOTPLUG_CPU
1184 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
1185 if (gov) {
1186 policy->governor = gov;
1187 pr_debug("Restoring governor %s for cpu %d\n",
1188 policy->governor->name, cpu);
Thomas Renninger4bfa0422009-07-24 15:25:03 +02001189 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001190#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301192 if (!frozen) {
Viresh Kumar308b60e2013-07-31 14:35:14 +02001193 ret = cpufreq_add_dev_interface(policy, dev);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301194 if (ret)
1195 goto err_out_unregister;
Viresh Kumarfcd7af92014-01-07 07:10:10 +05301196 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1197 CPUFREQ_CREATE_POLICY, policy);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301198 }
Dave Jones8ff69732006-03-05 03:37:23 -05001199
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301200 write_lock_irqsave(&cpufreq_driver_lock, flags);
1201 list_add(&policy->policy_list, &cpufreq_policy_list);
1202 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1203
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301204 cpufreq_init_policy(policy);
1205
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301206 if (!frozen) {
1207 policy->user_policy.policy = policy->policy;
1208 policy->user_policy.governor = policy->governor;
1209 }
Viresh Kumar4e97b632014-03-04 11:44:01 +08001210 up_write(&policy->rwsem);
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301211
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001212 kobject_uevent(&policy->kobj, KOBJ_ADD);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301213 up_read(&cpufreq_rwsem);
1214
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001215 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 return 0;
1218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219err_out_unregister:
Viresh Kumar652ed952014-01-09 20:38:43 +05301220err_get_freq:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001221 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar474deff2013-08-20 12:08:25 +05301222 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -07001223 per_cpu(cpufreq_cpu_data, j) = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001224 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Viresh Kumarda60ce92013-10-03 20:28:30 +05301226 if (cpufreq_driver->exit)
1227 cpufreq_driver->exit(policy);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301228err_set_policy_cpu:
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001229 if (frozen) {
1230 /* Do not leave stale fallback data behind. */
1231 per_cpu(cpufreq_cpu_data_fallback, cpu) = NULL;
Viresh Kumar42f921a2013-12-20 21:26:02 +05301232 cpufreq_policy_put_kobj(policy);
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001233 }
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301234 cpufreq_policy_free(policy);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236nomem_out:
Viresh Kumar6eed9402013-08-06 22:53:11 +05301237 up_read(&cpufreq_rwsem);
1238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return ret;
1240}
1241
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301242/**
1243 * cpufreq_add_dev - add a CPU device
1244 *
1245 * Adds the cpufreq interface for a CPU device.
1246 *
1247 * The Oracle says: try running cpufreq registration/unregistration concurrently
1248 * with with cpu hotplugging and all hell will break loose. Tried to clean this
1249 * mess up, but more thorough testing is needed. - Mathieu
1250 */
1251static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1252{
1253 return __cpufreq_add_dev(dev, sif, false);
1254}
1255
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301256static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy *policy,
Viresh Kumar42f921a2013-12-20 21:26:02 +05301257 unsigned int old_cpu)
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301258{
1259 struct device *cpu_dev;
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301260 int ret;
1261
1262 /* first sibling now owns the new sysfs dir */
Viresh Kumar9c8f1ee2013-09-12 17:06:33 +05301263 cpu_dev = get_cpu_device(cpumask_any_but(policy->cpus, old_cpu));
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301264
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301265 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301266 ret = kobject_move(&policy->kobj, &cpu_dev->kobj);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301267 if (ret) {
1268 pr_err("%s: Failed to move kobj: %d", __func__, ret);
1269
viresh kumarad7722d2013-10-18 19:10:15 +05301270 down_write(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301271 cpumask_set_cpu(old_cpu, policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301272 up_write(&policy->rwsem);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301273
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301274 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301275 "cpufreq");
1276
1277 return -EINVAL;
1278 }
1279
1280 return cpu_dev->id;
1281}
1282
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301283static int __cpufreq_remove_dev_prepare(struct device *dev,
1284 struct subsys_interface *sif,
1285 bool frozen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301287 unsigned int cpu = dev->id, cpus;
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301288 int new_cpu, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 unsigned long flags;
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301290 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001292 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001294 write_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301296 policy = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301298 /* Save the policy somewhere when doing a light-weight tear-down */
1299 if (frozen)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301300 per_cpu(cpufreq_cpu_data_fallback, cpu) = policy;
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301301
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001302 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301304 if (!policy) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001305 pr_debug("%s: No cpu_data found\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301309 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301310 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1311 if (ret) {
1312 pr_err("%s: Failed to stop governor\n", __func__);
1313 return ret;
1314 }
1315 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001316
Jacob Shin27ecddc2011-04-27 13:32:11 -05001317#ifdef CONFIG_HOTPLUG_CPU
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001318 if (!cpufreq_driver->setpolicy)
Dirk Brandewiefa69e332013-02-06 09:02:11 -08001319 strncpy(per_cpu(cpufreq_cpu_governor, cpu),
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301320 policy->governor->name, CPUFREQ_NAME_LEN);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001321#endif
1322
viresh kumarad7722d2013-10-18 19:10:15 +05301323 down_read(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301324 cpus = cpumask_weight(policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301325 up_read(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Srivatsa S. Bhat61173f22013-09-12 01:43:25 +05301327 if (cpu != policy->cpu) {
viresh kumar6964d912014-02-17 14:52:11 +05301328 sysfs_remove_link(&dev->kobj, "cpufreq");
Viresh Kumar73bf0fc2013-02-05 22:21:14 +01001329 } else if (cpus > 1) {
Viresh Kumar42f921a2013-12-20 21:26:02 +05301330 new_cpu = cpufreq_nominate_new_policy_cpu(policy, cpu);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301331 if (new_cpu >= 0) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301332 update_policy_cpu(policy, new_cpu);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301333
1334 if (!frozen) {
Viresh Kumar75949c92013-10-02 14:13:13 +05301335 pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
1336 __func__, new_cpu, cpu);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301337 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001338 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001339 }
Venki Pallipadiec282972007-03-26 12:03:19 -07001340
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301341 return 0;
1342}
1343
1344static int __cpufreq_remove_dev_finish(struct device *dev,
1345 struct subsys_interface *sif,
1346 bool frozen)
1347{
1348 unsigned int cpu = dev->id, cpus;
1349 int ret;
1350 unsigned long flags;
1351 struct cpufreq_policy *policy;
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301352
1353 read_lock_irqsave(&cpufreq_driver_lock, flags);
1354 policy = per_cpu(cpufreq_cpu_data, cpu);
1355 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1356
1357 if (!policy) {
1358 pr_debug("%s: No cpu_data found\n", __func__);
1359 return -EINVAL;
1360 }
1361
viresh kumarad7722d2013-10-18 19:10:15 +05301362 down_write(&policy->rwsem);
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301363 cpus = cpumask_weight(policy->cpus);
Viresh Kumar9c8f1ee2013-09-12 17:06:33 +05301364
1365 if (cpus > 1)
1366 cpumask_clear_cpu(cpu, policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301367 up_write(&policy->rwsem);
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301368
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001369 /* If cpu is last user of policy, free policy */
1370 if (cpus == 1) {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301371 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301372 ret = __cpufreq_governor(policy,
1373 CPUFREQ_GOV_POLICY_EXIT);
1374 if (ret) {
1375 pr_err("%s: Failed to exit governor\n",
1376 __func__);
1377 return ret;
1378 }
Viresh Kumaredab2fb2013-08-20 12:08:22 +05301379 }
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001380
Viresh Kumar42f921a2013-12-20 21:26:02 +05301381 if (!frozen)
1382 cpufreq_policy_put_kobj(policy);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301383
1384 /*
1385 * Perform the ->exit() even during light-weight tear-down,
1386 * since this is a core component, and is essential for the
1387 * subsequent light-weight ->init() to succeed.
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001388 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001389 if (cpufreq_driver->exit)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301390 cpufreq_driver->exit(policy);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001391
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301392 /* Remove policy from list of active policies */
1393 write_lock_irqsave(&cpufreq_driver_lock, flags);
1394 list_del(&policy->policy_list);
1395 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1396
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301397 if (!frozen)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301398 cpufreq_policy_free(policy);
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001399 } else {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301400 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301401 if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
1402 (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
1403 pr_err("%s: Failed to start governor\n",
1404 __func__);
1405 return ret;
1406 }
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001407 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Viresh Kumar474deff2013-08-20 12:08:25 +05301410 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 return 0;
1412}
1413
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301414/**
Viresh Kumar27a862e2013-10-02 14:13:14 +05301415 * cpufreq_remove_dev - remove a CPU device
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301416 *
1417 * Removes the cpufreq interface for a CPU device.
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301418 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001419static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001420{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001421 unsigned int cpu = dev->id;
Viresh Kumar27a862e2013-10-02 14:13:14 +05301422 int ret;
Venki Pallipadiec282972007-03-26 12:03:19 -07001423
1424 if (cpu_is_offline(cpu))
1425 return 0;
1426
Viresh Kumar27a862e2013-10-02 14:13:14 +05301427 ret = __cpufreq_remove_dev_prepare(dev, sif, false);
1428
1429 if (!ret)
1430 ret = __cpufreq_remove_dev_finish(dev, sif, false);
1431
1432 return ret;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001433}
1434
David Howells65f27f32006-11-22 14:55:48 +00001435static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
David Howells65f27f32006-11-22 14:55:48 +00001437 struct cpufreq_policy *policy =
1438 container_of(work, struct cpufreq_policy, update);
1439 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001440 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 cpufreq_update_policy(cpu);
1442}
1443
1444/**
Viresh Kumarbb176f72013-06-19 14:19:33 +05301445 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1446 * in deep trouble.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 * @cpu: cpu number
1448 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1449 * @new_freq: CPU frequency the CPU actually runs at
1450 *
Dave Jones29464f22009-01-18 01:37:11 -05001451 * We adjust to current frequency first, and need to clean up later.
1452 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301454static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1455 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301457 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301459 unsigned long flags;
1460
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001461 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 freqs.old = old_freq;
1465 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301466
1467 read_lock_irqsave(&cpufreq_driver_lock, flags);
1468 policy = per_cpu(cpufreq_cpu_data, cpu);
1469 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1470
1471 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1472 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473}
1474
Dave Jones32ee8c32006-02-28 00:43:23 -05001475/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301476 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001477 * @cpu: CPU number
1478 *
1479 * This is the last known freq, without actually getting it from the driver.
1480 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1481 */
1482unsigned int cpufreq_quick_get(unsigned int cpu)
1483{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001484 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301485 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001486
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001487 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1488 return cpufreq_driver->get(cpu);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001489
1490 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001491 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301492 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001493 cpufreq_cpu_put(policy);
1494 }
1495
Dave Jones4d34a672008-02-07 16:33:49 -05001496 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001497}
1498EXPORT_SYMBOL(cpufreq_quick_get);
1499
Jesse Barnes3d737102011-06-28 10:59:12 -07001500/**
1501 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1502 * @cpu: CPU number
1503 *
1504 * Just return the max possible frequency for a given CPU.
1505 */
1506unsigned int cpufreq_quick_get_max(unsigned int cpu)
1507{
1508 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1509 unsigned int ret_freq = 0;
1510
1511 if (policy) {
1512 ret_freq = policy->max;
1513 cpufreq_cpu_put(policy);
1514 }
1515
1516 return ret_freq;
1517}
1518EXPORT_SYMBOL(cpufreq_quick_get_max);
1519
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001520static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001522 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301523 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001525 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001526 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001528 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301530 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001531 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301532 /* verify no discrepancy between actual and
1533 saved value exists */
1534 if (unlikely(ret_freq != policy->cur)) {
1535 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 schedule_work(&policy->update);
1537 }
1538 }
1539
Dave Jones4d34a672008-02-07 16:33:49 -05001540 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001541}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001543/**
1544 * cpufreq_get - get the current CPU frequency (in kHz)
1545 * @cpu: CPU number
1546 *
1547 * Get the CPU current (static) CPU frequency
1548 */
1549unsigned int cpufreq_get(unsigned int cpu)
1550{
Aaron Plattner999976e2014-03-04 12:42:15 -08001551 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001552 unsigned int ret_freq = 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001553
Aaron Plattner999976e2014-03-04 12:42:15 -08001554 if (policy) {
1555 down_read(&policy->rwsem);
1556 ret_freq = __cpufreq_get(cpu);
1557 up_read(&policy->rwsem);
Viresh Kumar26ca8692013-09-20 22:37:31 +05301558
Aaron Plattner999976e2014-03-04 12:42:15 -08001559 cpufreq_cpu_put(policy);
1560 }
Viresh Kumar6eed9402013-08-06 22:53:11 +05301561
Dave Jones4d34a672008-02-07 16:33:49 -05001562 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563}
1564EXPORT_SYMBOL(cpufreq_get);
1565
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001566static struct subsys_interface cpufreq_interface = {
1567 .name = "cpufreq",
1568 .subsys = &cpu_subsys,
1569 .add_dev = cpufreq_add_dev,
1570 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001571};
1572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001574 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1575 *
1576 * This function is only executed for the boot processor. The other CPUs
1577 * have been put offline by means of CPU hotplug.
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001578 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001579static int cpufreq_bp_suspend(void)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001580{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301581 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001582
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001583 int cpu = smp_processor_id();
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301584 struct cpufreq_policy *policy;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001585
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001586 pr_debug("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001587
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001588 /* If there's no policy for the boot CPU, we have nothing to do. */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301589 policy = cpufreq_cpu_get(cpu);
1590 if (!policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001591 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001592
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001593 if (cpufreq_driver->suspend) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301594 ret = cpufreq_driver->suspend(policy);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001595 if (ret)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001596 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301597 "step on CPU %u\n", policy->cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001598 }
1599
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301600 cpufreq_cpu_put(policy);
Dave Jonesc9060492008-02-07 16:32:18 -05001601 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001602}
1603
1604/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001605 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 *
1607 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001608 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1609 * restored. It will verify that the current freq is in sync with
1610 * what we believe it to be. This is a bit later than when it
1611 * should be, but nonethteless it's better than calling
1612 * cpufreq_driver->get() here which might re-enable interrupts...
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001613 *
1614 * This function is only executed for the boot CPU. The other CPUs have not
1615 * been turned on yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001617static void cpufreq_bp_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301619 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001620
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001621 int cpu = smp_processor_id();
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301622 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001624 pr_debug("resuming cpu %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001626 /* If there's no policy for the boot CPU, we have nothing to do. */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301627 policy = cpufreq_cpu_get(cpu);
1628 if (!policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001629 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001631 if (cpufreq_driver->resume) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301632 ret = cpufreq_driver->resume(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 if (ret) {
1634 printk(KERN_ERR "cpufreq: resume failed in ->resume "
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301635 "step on CPU %u\n", policy->cpu);
Dave Jonesc9060492008-02-07 16:32:18 -05001636 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 }
1638 }
1639
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301640 schedule_work(&policy->update);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001641
Dave Jonesc9060492008-02-07 16:32:18 -05001642fail:
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301643 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644}
1645
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001646static struct syscore_ops cpufreq_syscore_ops = {
1647 .suspend = cpufreq_bp_suspend,
1648 .resume = cpufreq_bp_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649};
1650
Borislav Petkov9d950462013-01-20 10:24:28 +00001651/**
1652 * cpufreq_get_current_driver - return current driver's name
1653 *
1654 * Return the name string of the currently loaded cpufreq driver
1655 * or NULL, if none.
1656 */
1657const char *cpufreq_get_current_driver(void)
1658{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001659 if (cpufreq_driver)
1660 return cpufreq_driver->name;
1661
1662 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001663}
1664EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
1666/*********************************************************************
1667 * NOTIFIER LISTS INTERFACE *
1668 *********************************************************************/
1669
1670/**
1671 * cpufreq_register_notifier - register a driver with cpufreq
1672 * @nb: notifier function to register
1673 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1674 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001675 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 * are notified about clock rate changes (once before and once after
1677 * the transition), or a list of drivers that are notified about
1678 * changes in cpufreq policy.
1679 *
1680 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001681 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 */
1683int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1684{
1685 int ret;
1686
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001687 if (cpufreq_disabled())
1688 return -EINVAL;
1689
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001690 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 switch (list) {
1693 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001694 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001695 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 break;
1697 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001698 ret = blocking_notifier_chain_register(
1699 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 break;
1701 default:
1702 ret = -EINVAL;
1703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
1705 return ret;
1706}
1707EXPORT_SYMBOL(cpufreq_register_notifier);
1708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709/**
1710 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1711 * @nb: notifier block to be unregistered
Viresh Kumarbb176f72013-06-19 14:19:33 +05301712 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 *
1714 * Remove a driver from the CPU frequency notifier list.
1715 *
1716 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001717 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 */
1719int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1720{
1721 int ret;
1722
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001723 if (cpufreq_disabled())
1724 return -EINVAL;
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 switch (list) {
1727 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001728 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001729 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 break;
1731 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001732 ret = blocking_notifier_chain_unregister(
1733 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 break;
1735 default:
1736 ret = -EINVAL;
1737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 return ret;
1740}
1741EXPORT_SYMBOL(cpufreq_unregister_notifier);
1742
1743
1744/*********************************************************************
1745 * GOVERNORS *
1746 *********************************************************************/
1747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748int __cpufreq_driver_target(struct cpufreq_policy *policy,
1749 unsigned int target_freq,
1750 unsigned int relation)
1751{
1752 int retval = -EINVAL;
Viresh Kumar72499242012-10-31 01:28:21 +01001753 unsigned int old_target_freq = target_freq;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001754
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001755 if (cpufreq_disabled())
1756 return -ENODEV;
1757
Viresh Kumar72499242012-10-31 01:28:21 +01001758 /* Make sure that target_freq is within supported range */
1759 if (target_freq > policy->max)
1760 target_freq = policy->max;
1761 if (target_freq < policy->min)
1762 target_freq = policy->min;
1763
1764 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1765 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001766
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301767 /*
1768 * This might look like a redundant call as we are checking it again
1769 * after finding index. But it is left intentionally for cases where
1770 * exactly same freq is called again and so we can save on few function
1771 * calls.
1772 */
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001773 if (target_freq == policy->cur)
1774 return 0;
1775
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001776 if (cpufreq_driver->target)
1777 retval = cpufreq_driver->target(policy, target_freq, relation);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301778 else if (cpufreq_driver->target_index) {
1779 struct cpufreq_frequency_table *freq_table;
Viresh Kumard4019f02013-08-14 19:38:24 +05301780 struct cpufreq_freqs freqs;
1781 bool notify;
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301782 int index;
Ashok Raj90d45d12005-11-08 21:34:24 -08001783
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301784 freq_table = cpufreq_frequency_get_table(policy->cpu);
1785 if (unlikely(!freq_table)) {
1786 pr_err("%s: Unable to find freq_table\n", __func__);
1787 goto out;
1788 }
1789
1790 retval = cpufreq_frequency_table_target(policy, freq_table,
1791 target_freq, relation, &index);
1792 if (unlikely(retval)) {
1793 pr_err("%s: Unable to find matching freq\n", __func__);
1794 goto out;
1795 }
1796
Viresh Kumard4019f02013-08-14 19:38:24 +05301797 if (freq_table[index].frequency == policy->cur) {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301798 retval = 0;
Viresh Kumard4019f02013-08-14 19:38:24 +05301799 goto out;
1800 }
1801
1802 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
1803
1804 if (notify) {
1805 freqs.old = policy->cur;
1806 freqs.new = freq_table[index].frequency;
1807 freqs.flags = 0;
1808
1809 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1810 __func__, policy->cpu, freqs.old,
1811 freqs.new);
1812
1813 cpufreq_notify_transition(policy, &freqs,
1814 CPUFREQ_PRECHANGE);
1815 }
1816
1817 retval = cpufreq_driver->target_index(policy, index);
1818 if (retval)
1819 pr_err("%s: Failed to change cpu frequency: %d\n",
1820 __func__, retval);
1821
Viresh Kumarab1b1c42013-12-02 11:04:13 +05301822 if (notify)
1823 cpufreq_notify_post_transition(policy, &freqs, retval);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301824 }
1825
1826out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 return retval;
1828}
1829EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831int cpufreq_driver_target(struct cpufreq_policy *policy,
1832 unsigned int target_freq,
1833 unsigned int relation)
1834{
Julia Lawallf1829e42008-07-25 22:44:53 +02001835 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
viresh kumarad7722d2013-10-18 19:10:15 +05301837 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
1839 ret = __cpufreq_driver_target(policy, target_freq, relation);
1840
viresh kumarad7722d2013-10-18 19:10:15 +05301841 up_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 return ret;
1844}
1845EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1846
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001847/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001848 * when "event" is CPUFREQ_GOV_LIMITS
1849 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301851static int __cpufreq_governor(struct cpufreq_policy *policy,
1852 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
Dave Jonescc993ca2005-07-28 09:43:56 -07001854 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001855
1856 /* Only must be defined when default governor is known to have latency
1857 restrictions, like e.g. conservative or ondemand.
1858 That this is the case is already ensured in Kconfig
1859 */
1860#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1861 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1862#else
1863 struct cpufreq_governor *gov = NULL;
1864#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001865
1866 if (policy->governor->max_transition_latency &&
1867 policy->cpuinfo.transition_latency >
1868 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07001869 if (!gov)
1870 return -EINVAL;
1871 else {
1872 printk(KERN_WARNING "%s governor failed, too long"
1873 " transition latency of HW, fallback"
1874 " to %s governor\n",
1875 policy->governor->name,
1876 gov->name);
1877 policy->governor = gov;
1878 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Viresh Kumarfe492f32013-08-06 22:53:10 +05301881 if (event == CPUFREQ_GOV_POLICY_INIT)
1882 if (!try_module_get(policy->governor->owner))
1883 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001885 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301886 policy->cpu, event);
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001887
1888 mutex_lock(&cpufreq_governor_lock);
Srivatsa S. Bhat56d07db2013-09-07 01:23:55 +05301889 if ((policy->governor_enabled && event == CPUFREQ_GOV_START)
Viresh Kumarf73d3932013-08-31 17:53:40 +05301890 || (!policy->governor_enabled
1891 && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) {
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001892 mutex_unlock(&cpufreq_governor_lock);
1893 return -EBUSY;
1894 }
1895
1896 if (event == CPUFREQ_GOV_STOP)
1897 policy->governor_enabled = false;
1898 else if (event == CPUFREQ_GOV_START)
1899 policy->governor_enabled = true;
1900
1901 mutex_unlock(&cpufreq_governor_lock);
1902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 ret = policy->governor->governor(policy, event);
1904
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001905 if (!ret) {
1906 if (event == CPUFREQ_GOV_POLICY_INIT)
1907 policy->governor->initialized++;
1908 else if (event == CPUFREQ_GOV_POLICY_EXIT)
1909 policy->governor->initialized--;
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001910 } else {
1911 /* Restore original values */
1912 mutex_lock(&cpufreq_governor_lock);
1913 if (event == CPUFREQ_GOV_STOP)
1914 policy->governor_enabled = true;
1915 else if (event == CPUFREQ_GOV_START)
1916 policy->governor_enabled = false;
1917 mutex_unlock(&cpufreq_governor_lock);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001918 }
Viresh Kumarb3940582013-02-01 05:42:58 +00001919
Viresh Kumarfe492f32013-08-06 22:53:10 +05301920 if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
1921 ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 module_put(policy->governor->owner);
1923
1924 return ret;
1925}
1926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927int cpufreq_register_governor(struct cpufreq_governor *governor)
1928{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001929 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
1931 if (!governor)
1932 return -EINVAL;
1933
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001934 if (cpufreq_disabled())
1935 return -ENODEV;
1936
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001937 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001938
Viresh Kumarb3940582013-02-01 05:42:58 +00001939 governor->initialized = 0;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001940 err = -EBUSY;
1941 if (__find_governor(governor->name) == NULL) {
1942 err = 0;
1943 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Dave Jones32ee8c32006-02-28 00:43:23 -05001946 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001947 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948}
1949EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1952{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001953#ifdef CONFIG_HOTPLUG_CPU
1954 int cpu;
1955#endif
1956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 if (!governor)
1958 return;
1959
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001960 if (cpufreq_disabled())
1961 return;
1962
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001963#ifdef CONFIG_HOTPLUG_CPU
1964 for_each_present_cpu(cpu) {
1965 if (cpu_online(cpu))
1966 continue;
1967 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1968 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1969 }
1970#endif
1971
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001972 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001974 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 return;
1976}
1977EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1978
1979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980/*********************************************************************
1981 * POLICY INTERFACE *
1982 *********************************************************************/
1983
1984/**
1985 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05001986 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1987 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 *
1989 * Reads the current cpufreq policy.
1990 */
1991int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1992{
1993 struct cpufreq_policy *cpu_policy;
1994 if (!policy)
1995 return -EINVAL;
1996
1997 cpu_policy = cpufreq_cpu_get(cpu);
1998 if (!cpu_policy)
1999 return -EINVAL;
2000
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302001 memcpy(policy, cpu_policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
2003 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 return 0;
2005}
2006EXPORT_SYMBOL(cpufreq_get_policy);
2007
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002008/*
Viresh Kumar037ce832013-10-02 14:13:16 +05302009 * policy : current policy.
2010 * new_policy: policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002011 */
Viresh Kumar037ce832013-10-02 14:13:16 +05302012static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302013 struct cpufreq_policy *new_policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014{
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002015 int ret = 0, failed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302017 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", new_policy->cpu,
2018 new_policy->min, new_policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302020 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302022 if (new_policy->min > policy->max || new_policy->max < policy->min) {
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02002023 ret = -EINVAL;
2024 goto error_out;
2025 }
2026
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 /* verify the cpu speed can be set within this limit */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302028 ret = cpufreq_driver->verify(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 if (ret)
2030 goto error_out;
2031
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08002033 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302034 CPUFREQ_ADJUST, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
2036 /* adjust if necessary - hardware incompatibility*/
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_INCOMPATIBLE, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
Viresh Kumarbb176f72013-06-19 14:19:33 +05302040 /*
2041 * verify the cpu speed can be set within this limit, which might be
2042 * different to the first one
2043 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302044 ret = cpufreq_driver->verify(new_policy);
Alan Sterne041c682006-03-27 01:16:30 -08002045 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
2048 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08002049 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302050 CPUFREQ_NOTIFY, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302052 policy->min = new_policy->min;
2053 policy->max = new_policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002055 pr_debug("new min and max freqs are %u - %u kHz\n",
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302056 policy->min, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002058 if (cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302059 policy->policy = new_policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002060 pr_debug("setting range\n");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302061 ret = cpufreq_driver->setpolicy(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 } else {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302063 if (new_policy->governor != policy->governor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 /* save old, working values */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302065 struct cpufreq_governor *old_gov = policy->governor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002067 pr_debug("governor switch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
2069 /* end old governor */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302070 if (policy->governor) {
2071 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
viresh kumarad7722d2013-10-18 19:10:15 +05302072 up_write(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302073 __cpufreq_governor(policy,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002074 CPUFREQ_GOV_POLICY_EXIT);
viresh kumarad7722d2013-10-18 19:10:15 +05302075 down_write(&policy->rwsem);
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
2078 /* start new governor */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302079 policy->governor = new_policy->governor;
2080 if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
2081 if (!__cpufreq_governor(policy, CPUFREQ_GOV_START)) {
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002082 failed = 0;
Viresh Kumar955ef482013-05-16 05:09:58 +00002083 } else {
viresh kumarad7722d2013-10-18 19:10:15 +05302084 up_write(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302085 __cpufreq_governor(policy,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002086 CPUFREQ_GOV_POLICY_EXIT);
viresh kumarad7722d2013-10-18 19:10:15 +05302087 down_write(&policy->rwsem);
Viresh Kumar955ef482013-05-16 05:09:58 +00002088 }
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002089 }
2090
2091 if (failed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 /* new governor failed, so re-start old one */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002093 pr_debug("starting governor %s failed\n",
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302094 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 if (old_gov) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302096 policy->governor = old_gov;
2097 __cpufreq_governor(policy,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002098 CPUFREQ_GOV_POLICY_INIT);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302099 __cpufreq_governor(policy,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05302100 CPUFREQ_GOV_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 }
2102 ret = -EINVAL;
2103 goto error_out;
2104 }
2105 /* might be a policy change, too, so fall through */
2106 }
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002107 pr_debug("governor: change or update limits\n");
Viresh Kumar3de9bde2013-08-06 22:53:13 +05302108 ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 }
2110
Dave Jones7d5e3502006-02-02 17:03:42 -05002111error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 return ret;
2113}
2114
2115/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
2117 * @cpu: CPU which shall be re-evaluated
2118 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002119 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 * at different times.
2121 */
2122int cpufreq_update_policy(unsigned int cpu)
2123{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302124 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2125 struct cpufreq_policy new_policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02002126 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302128 if (!policy) {
Julia Lawallf1829e42008-07-25 22:44:53 +02002129 ret = -ENODEV;
2130 goto no_policy;
2131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
viresh kumarad7722d2013-10-18 19:10:15 +05302133 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002135 pr_debug("updating policy for CPU %u\n", cpu);
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302136 memcpy(&new_policy, policy, sizeof(*policy));
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302137 new_policy.min = policy->user_policy.min;
2138 new_policy.max = policy->user_policy.max;
2139 new_policy.policy = policy->user_policy.policy;
2140 new_policy.governor = policy->user_policy.governor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
Viresh Kumarbb176f72013-06-19 14:19:33 +05302142 /*
2143 * BIOS might change freq behind our back
2144 * -> ask driver for current freq and notify governors about a change
2145 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002146 if (cpufreq_driver->get) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302147 new_policy.cur = cpufreq_driver->get(cpu);
2148 if (!policy->cur) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002149 pr_debug("Driver did not initialize current freq");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302150 policy->cur = new_policy.cur;
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002151 } else {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302152 if (policy->cur != new_policy.cur && has_target())
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302153 cpufreq_out_of_sync(cpu, policy->cur,
2154 new_policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002155 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01002156 }
2157
Viresh Kumar037ce832013-10-02 14:13:16 +05302158 ret = cpufreq_set_policy(policy, &new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
viresh kumarad7722d2013-10-18 19:10:15 +05302160 up_write(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002161
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302162 cpufreq_cpu_put(policy);
Julia Lawallf1829e42008-07-25 22:44:53 +02002163no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 return ret;
2165}
2166EXPORT_SYMBOL(cpufreq_update_policy);
2167
Paul Gortmaker27609842013-06-19 13:54:04 -04002168static int cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002169 unsigned long action, void *hcpu)
2170{
2171 unsigned int cpu = (unsigned long)hcpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002172 struct device *dev;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302173 bool frozen = false;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002174
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002175 dev = get_cpu_device(cpu);
2176 if (dev) {
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302177
Rafael J. Wysockid4faadd2013-12-08 01:23:58 +01002178 if (action & CPU_TASKS_FROZEN)
2179 frozen = true;
2180
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302181 switch (action & ~CPU_TASKS_FROZEN) {
Ashok Rajc32b6b82005-10-30 14:59:54 -08002182 case CPU_ONLINE:
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302183 __cpufreq_add_dev(dev, NULL, frozen);
Srivatsa S. Bhat23d328992013-07-30 04:23:56 +05302184 cpufreq_update_policy(cpu);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002185 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302186
Ashok Rajc32b6b82005-10-30 14:59:54 -08002187 case CPU_DOWN_PREPARE:
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05302188 __cpufreq_remove_dev_prepare(dev, NULL, frozen);
Srivatsa S. Bhat1aee40a2013-09-07 01:23:27 +05302189 break;
2190
2191 case CPU_POST_DEAD:
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05302192 __cpufreq_remove_dev_finish(dev, NULL, frozen);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002193 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302194
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002195 case CPU_DOWN_FAILED:
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302196 __cpufreq_add_dev(dev, NULL, frozen);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002197 break;
2198 }
2199 }
2200 return NOTIFY_OK;
2201}
2202
Neal Buckendahl9c36f742010-06-22 22:02:44 -05002203static struct notifier_block __refdata cpufreq_cpu_notifier = {
Viresh Kumarbb176f72013-06-19 14:19:33 +05302204 .notifier_call = cpufreq_cpu_callback,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002205};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
2207/*********************************************************************
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002208 * BOOST *
2209 *********************************************************************/
2210static int cpufreq_boost_set_sw(int state)
2211{
2212 struct cpufreq_frequency_table *freq_table;
2213 struct cpufreq_policy *policy;
2214 int ret = -EINVAL;
2215
2216 list_for_each_entry(policy, &cpufreq_policy_list, policy_list) {
2217 freq_table = cpufreq_frequency_get_table(policy->cpu);
2218 if (freq_table) {
2219 ret = cpufreq_frequency_table_cpuinfo(policy,
2220 freq_table);
2221 if (ret) {
2222 pr_err("%s: Policy frequency update failed\n",
2223 __func__);
2224 break;
2225 }
2226 policy->user_policy.max = policy->max;
2227 __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
2228 }
2229 }
2230
2231 return ret;
2232}
2233
2234int cpufreq_boost_trigger_state(int state)
2235{
2236 unsigned long flags;
2237 int ret = 0;
2238
2239 if (cpufreq_driver->boost_enabled == state)
2240 return 0;
2241
2242 write_lock_irqsave(&cpufreq_driver_lock, flags);
2243 cpufreq_driver->boost_enabled = state;
2244 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2245
2246 ret = cpufreq_driver->set_boost(state);
2247 if (ret) {
2248 write_lock_irqsave(&cpufreq_driver_lock, flags);
2249 cpufreq_driver->boost_enabled = !state;
2250 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2251
2252 pr_err("%s: Cannot %s BOOST\n", __func__,
2253 state ? "enable" : "disable");
2254 }
2255
2256 return ret;
2257}
2258
2259int cpufreq_boost_supported(void)
2260{
2261 if (likely(cpufreq_driver))
2262 return cpufreq_driver->boost_supported;
2263
2264 return 0;
2265}
2266EXPORT_SYMBOL_GPL(cpufreq_boost_supported);
2267
2268int cpufreq_boost_enabled(void)
2269{
2270 return cpufreq_driver->boost_enabled;
2271}
2272EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2273
2274/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2276 *********************************************************************/
2277
2278/**
2279 * cpufreq_register_driver - register a CPU Frequency driver
2280 * @driver_data: A struct cpufreq_driver containing the values#
2281 * submitted by the CPU Frequency driver.
2282 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302283 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05002285 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 *
2287 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002288int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289{
2290 unsigned long flags;
2291 int ret;
2292
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002293 if (cpufreq_disabled())
2294 return -ENODEV;
2295
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 if (!driver_data || !driver_data->verify || !driver_data->init ||
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302297 !(driver_data->setpolicy || driver_data->target_index ||
2298 driver_data->target))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 return -EINVAL;
2300
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002301 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
2303 if (driver_data->setpolicy)
2304 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2305
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002306 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002307 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002308 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Yinghai Lu4dea58062013-09-18 21:05:20 -07002309 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002311 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002312 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002314 if (cpufreq_boost_supported()) {
2315 /*
2316 * Check if driver provides function to enable boost -
2317 * if not, use cpufreq_boost_set_sw as default
2318 */
2319 if (!cpufreq_driver->set_boost)
2320 cpufreq_driver->set_boost = cpufreq_boost_set_sw;
2321
2322 ret = cpufreq_sysfs_create_file(&boost.attr);
2323 if (ret) {
2324 pr_err("%s: cannot register global BOOST sysfs file\n",
2325 __func__);
2326 goto err_null_driver;
2327 }
2328 }
2329
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002330 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002331 if (ret)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002332 goto err_boost_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002334 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 int i;
2336 ret = -ENODEV;
2337
2338 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07002339 for (i = 0; i < nr_cpu_ids; i++)
2340 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07002342 break;
2343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
2345 /* if all ->init() calls failed, unregister */
2346 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002347 pr_debug("no CPU initialized for driver %s\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05302348 driver_data->name);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002349 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 }
2351 }
2352
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002353 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002354 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002356 return 0;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002357err_if_unreg:
2358 subsys_interface_unregister(&cpufreq_interface);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002359err_boost_unreg:
2360 if (cpufreq_boost_supported())
2361 cpufreq_sysfs_remove_file(&boost.attr);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002362err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002363 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002364 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002365 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05002366 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367}
2368EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2369
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370/**
2371 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2372 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302373 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 * the right to do so, i.e. if you have succeeded in initialising before!
2375 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2376 * currently not initialised.
2377 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002378int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379{
2380 unsigned long flags;
2381
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002382 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002385 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002387 subsys_interface_unregister(&cpufreq_interface);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002388 if (cpufreq_boost_supported())
2389 cpufreq_sysfs_remove_file(&boost.attr);
2390
Chandra Seetharaman65edc682006-06-27 02:54:08 -07002391 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
Viresh Kumar6eed9402013-08-06 22:53:11 +05302393 down_write(&cpufreq_rwsem);
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002394 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302395
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002396 cpufreq_driver = NULL;
Viresh Kumar6eed9402013-08-06 22:53:11 +05302397
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002398 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302399 up_write(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
2401 return 0;
2402}
2403EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002404
2405static int __init cpufreq_core_init(void)
2406{
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002407 if (cpufreq_disabled())
2408 return -ENODEV;
2409
Viresh Kumar2361be22013-05-17 16:09:09 +05302410 cpufreq_global_kobject = kobject_create();
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002411 BUG_ON(!cpufreq_global_kobject);
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01002412 register_syscore_ops(&cpufreq_syscore_ops);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002413
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002414 return 0;
2415}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002416core_initcall(cpufreq_core_init);