blob: 08ca8c9f41cdeb27f6e4bb4879b2912798e1a72e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/cpufreq/cpufreq.c
3 *
4 * Copyright (C) 2001 Russell King
5 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
Viresh Kumarbb176f72013-06-19 14:19:33 +05306 * (C) 2013 Viresh Kumar <viresh.kumar@linaro.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Ashok Rajc32b6b82005-10-30 14:59:54 -08008 * Oct 2005 - Ashok Raj <ashok.raj@intel.com>
Dave Jones32ee8c32006-02-28 00:43:23 -05009 * Added handling for CPU hotplug
Dave Jones8ff69732006-03-05 03:37:23 -050010 * Feb 2006 - Jacob Shin <jacob.shin@amd.com>
11 * Fix handling for CPU hotplug -- affected CPUs
Ashok Rajc32b6b82005-10-30 14:59:54 -080012 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Viresh Kumardb701152012-10-23 01:29:03 +020018#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
Viresh Kumar5ff0a262013-08-06 22:53:03 +053020#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/cpufreq.h>
22#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/device.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053024#include <linux/init.h>
25#include <linux/kernel_stat.h>
26#include <linux/module.h>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080027#include <linux/mutex.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053028#include <linux/slab.h>
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +010029#include <linux/syscore_ops.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053030#include <linux/tick.h>
Thomas Renninger6f4f2722010-04-20 13:17:36 +020031#include <trace/events/power.h>
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/**
Dave Jonescd878472006-08-11 17:59:28 -040034 * The "cpufreq driver" - the arch- or hardware-dependent low
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 * level driver of CPUFreq support, and its spinlock. This lock
36 * also protects the cpufreq_cpu_data array.
37 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +020038static struct cpufreq_driver *cpufreq_driver;
Mike Travis7a6aedf2008-03-25 15:06:53 -070039static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +053040static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data_fallback);
Viresh Kumarbb176f72013-06-19 14:19:33 +053041static DEFINE_RWLOCK(cpufreq_driver_lock);
Jane Li6f1e4ef2014-01-03 17:17:41 +080042DEFINE_MUTEX(cpufreq_governor_lock);
Lukasz Majewskic88a1f82013-08-06 22:53:08 +053043static LIST_HEAD(cpufreq_policy_list);
Viresh Kumarbb176f72013-06-19 14:19:33 +053044
Thomas Renninger084f3492007-07-09 11:35:28 -070045#ifdef CONFIG_HOTPLUG_CPU
46/* This one keeps track of the previously set governor of a removed CPU */
Dmitry Monakhove77b89f2009-10-05 00:38:55 +040047static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
Thomas Renninger084f3492007-07-09 11:35:28 -070048#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +053050static inline bool has_target(void)
51{
52 return cpufreq_driver->target_index || cpufreq_driver->target;
53}
54
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080055/*
Viresh Kumar6eed9402013-08-06 22:53:11 +053056 * rwsem to guarantee that cpufreq driver module doesn't unload during critical
57 * sections
58 */
59static DECLARE_RWSEM(cpufreq_rwsem);
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/* internal prototypes */
Dave Jones29464f22009-01-18 01:37:11 -050062static int __cpufreq_governor(struct cpufreq_policy *policy,
63 unsigned int event);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080064static unsigned int __cpufreq_get(unsigned int cpu);
David Howells65f27f32006-11-22 14:55:48 +000065static void handle_update(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67/**
Dave Jones32ee8c32006-02-28 00:43:23 -050068 * Two notifier lists: the "policy" list is involved in the
69 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 * "transition" list for kernel code that needs to handle
71 * changes to devices when the CPU clock speed changes.
72 * The mutex locks both lists.
73 */
Alan Sterne041c682006-03-27 01:16:30 -080074static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -070075static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -020077static bool init_cpufreq_transition_notifier_list_called;
Alan Sternb4dfdbb2006-10-04 02:17:06 -070078static int __init init_cpufreq_transition_notifier_list(void)
79{
80 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -020081 init_cpufreq_transition_notifier_list_called = true;
Alan Sternb4dfdbb2006-10-04 02:17:06 -070082 return 0;
83}
Linus Torvaldsb3438f82006-11-20 11:47:18 -080084pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -040086static int off __read_mostly;
Viresh Kumarda584452012-10-26 00:51:32 +020087static int cpufreq_disabled(void)
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -040088{
89 return off;
90}
91void disable_cpufreq(void)
92{
93 off = 1;
94}
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static LIST_HEAD(cpufreq_governor_list);
Dave Jones29464f22009-01-18 01:37:11 -050096static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000098bool have_governor_per_policy(void)
99{
Viresh Kumar0b981e72013-10-02 14:13:18 +0530100 return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000101}
Viresh Kumar3f869d62013-05-16 05:09:56 +0000102EXPORT_SYMBOL_GPL(have_governor_per_policy);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000103
Viresh Kumar944e9a02013-05-16 05:09:57 +0000104struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
105{
106 if (have_governor_per_policy())
107 return &policy->kobj;
108 else
109 return cpufreq_global_kobject;
110}
111EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
112
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000113static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
114{
115 u64 idle_time;
116 u64 cur_wall_time;
117 u64 busy_time;
118
119 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
120
121 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
122 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
123 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
124 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
125 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
126 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
127
128 idle_time = cur_wall_time - busy_time;
129 if (wall)
130 *wall = cputime_to_usecs(cur_wall_time);
131
132 return cputime_to_usecs(idle_time);
133}
134
135u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
136{
137 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
138
139 if (idle_time == -1ULL)
140 return get_cpu_idle_time_jiffy(cpu, wall);
141 else if (!io_busy)
142 idle_time += get_cpu_iowait_time_us(cpu, wall);
143
144 return idle_time;
145}
146EXPORT_SYMBOL_GPL(get_cpu_idle_time);
147
Viresh Kumar70e9e772013-10-03 20:29:07 +0530148/*
149 * This is a generic cpufreq init() routine which can be used by cpufreq
150 * drivers of SMP systems. It will do following:
151 * - validate & show freq table passed
152 * - set policies transition latency
153 * - policy->cpus with all possible CPUs
154 */
155int cpufreq_generic_init(struct cpufreq_policy *policy,
156 struct cpufreq_frequency_table *table,
157 unsigned int transition_latency)
158{
159 int ret;
160
161 ret = cpufreq_table_validate_and_show(policy, table);
162 if (ret) {
163 pr_err("%s: invalid frequency table: %d\n", __func__, ret);
164 return ret;
165 }
166
167 policy->cpuinfo.transition_latency = transition_latency;
168
169 /*
170 * The driver only supports the SMP configuartion where all processors
171 * share the clock and voltage and clock.
172 */
173 cpumask_setall(policy->cpus);
174
175 return 0;
176}
177EXPORT_SYMBOL_GPL(cpufreq_generic_init);
178
Viresh Kumar652ed952014-01-09 20:38:43 +0530179unsigned int cpufreq_generic_get(unsigned int cpu)
180{
181 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
182
183 if (!policy || IS_ERR(policy->clk)) {
184 pr_err("%s: No %s associated to cpu: %d\n", __func__,
185 policy ? "clk" : "policy", cpu);
186 return 0;
187 }
188
189 return clk_get_rate(policy->clk) / 1000;
190}
191EXPORT_SYMBOL_GPL(cpufreq_generic_get);
192
Viresh Kumar6eed9402013-08-06 22:53:11 +0530193struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530195 struct cpufreq_policy *policy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 unsigned long flags;
197
Viresh Kumar6eed9402013-08-06 22:53:11 +0530198 if (cpufreq_disabled() || (cpu >= nr_cpu_ids))
199 return NULL;
200
201 if (!down_read_trylock(&cpufreq_rwsem))
202 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 /* get the cpufreq driver */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000205 read_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Viresh Kumar6eed9402013-08-06 22:53:11 +0530207 if (cpufreq_driver) {
208 /* get the CPU */
209 policy = per_cpu(cpufreq_cpu_data, cpu);
210 if (policy)
211 kobject_get(&policy->kobj);
212 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200213
Viresh Kumar6eed9402013-08-06 22:53:11 +0530214 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530216 if (!policy)
Viresh Kumar6eed9402013-08-06 22:53:11 +0530217 up_read(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530219 return policy;
Stephen Boyda9144432012-07-20 18:14:38 +0000220}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
222
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530223void cpufreq_cpu_put(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000225 if (cpufreq_disabled())
226 return;
227
Viresh Kumar6eed9402013-08-06 22:53:11 +0530228 kobject_put(&policy->kobj);
229 up_read(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
235 *********************************************************************/
236
237/**
238 * adjust_jiffies - adjust the system "loops_per_jiffy"
239 *
240 * This function alters the system "loops_per_jiffy" for the clock
241 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500242 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 * per-CPU loops_per_jiffy value wherever possible.
244 */
245#ifndef CONFIG_SMP
246static unsigned long l_p_j_ref;
Viresh Kumarbb176f72013-06-19 14:19:33 +0530247static unsigned int l_p_j_ref_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Arjan van de Ven858119e2006-01-14 13:20:43 -0800249static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 if (ci->flags & CPUFREQ_CONST_LOOPS)
252 return;
253
254 if (!l_p_j_ref_freq) {
255 l_p_j_ref = loops_per_jiffy;
256 l_p_j_ref_freq = ci->old;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200257 pr_debug("saving %lu as reference value for loops_per_jiffy; "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530258 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
Viresh Kumarbb176f72013-06-19 14:19:33 +0530260 if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700261 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530262 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
263 ci->new);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200264 pr_debug("scaling loops_per_jiffy to %lu "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530265 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267}
268#else
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530269static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
270{
271 return;
272}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273#endif
274
Viresh Kumar0956df9c2013-06-19 14:19:34 +0530275static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530276 struct cpufreq_freqs *freqs, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 BUG_ON(irqs_disabled());
279
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000280 if (cpufreq_disabled())
281 return;
282
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200283 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200284 pr_debug("notification %u of frequency transition to %u kHz\n",
Dave Jonese4472cb2006-01-31 15:53:55 -0800285 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500290 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800291 * which is not equal to what the cpufreq core thinks is
292 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200294 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800295 if ((policy) && (policy->cpu == freqs->cpu) &&
296 (policy->cur) && (policy->cur != freqs->old)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200297 pr_debug("Warning: CPU frequency is"
Dave Jonese4472cb2006-01-31 15:53:55 -0800298 " %u, cpufreq assumed %u kHz.\n",
299 freqs->old, policy->cur);
300 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700303 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800304 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
306 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 case CPUFREQ_POSTCHANGE:
309 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200310 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
Thomas Renninger6f4f2722010-04-20 13:17:36 +0200311 (unsigned long)freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100312 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700313 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800314 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800315 if (likely(policy) && likely(policy->cpu == freqs->cpu))
316 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 break;
318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
Viresh Kumarbb176f72013-06-19 14:19:33 +0530320
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530321/**
322 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
323 * on frequency transition.
324 *
325 * This function calls the transition notifiers and the "adjust_jiffies"
326 * function. It is called twice on all CPU frequency changes that have
327 * external effects.
328 */
329void cpufreq_notify_transition(struct cpufreq_policy *policy,
330 struct cpufreq_freqs *freqs, unsigned int state)
331{
332 for_each_cpu(freqs->cpu, policy->cpus)
333 __cpufreq_notify_transition(policy, freqs, state);
334}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
336
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530337/* Do post notifications when there are chances that transition has failed */
338void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
339 struct cpufreq_freqs *freqs, int transition_failed)
340{
341 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
342 if (!transition_failed)
343 return;
344
345 swap(freqs->old, freqs->new);
346 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
347 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
348}
349EXPORT_SYMBOL_GPL(cpufreq_notify_post_transition);
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352/*********************************************************************
353 * SYSFS INTERFACE *
354 *********************************************************************/
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100355ssize_t show_boost(struct kobject *kobj,
356 struct attribute *attr, char *buf)
357{
358 return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
359}
360
361static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
362 const char *buf, size_t count)
363{
364 int ret, enable;
365
366 ret = sscanf(buf, "%d", &enable);
367 if (ret != 1 || enable < 0 || enable > 1)
368 return -EINVAL;
369
370 if (cpufreq_boost_trigger_state(enable)) {
371 pr_err("%s: Cannot %s BOOST!\n", __func__,
372 enable ? "enable" : "disable");
373 return -EINVAL;
374 }
375
376 pr_debug("%s: cpufreq BOOST %s\n", __func__,
377 enable ? "enabled" : "disabled");
378
379 return count;
380}
381define_one_global_rw(boost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700383static struct cpufreq_governor *__find_governor(const char *str_governor)
384{
385 struct cpufreq_governor *t;
386
387 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
Dave Jones29464f22009-01-18 01:37:11 -0500388 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700389 return t;
390
391 return NULL;
392}
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394/**
395 * cpufreq_parse_governor - parse a governor string
396 */
Dave Jones905d77c2008-03-05 14:28:32 -0500397static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 struct cpufreq_governor **governor)
399{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700400 int err = -EINVAL;
401
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200402 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700403 goto out;
404
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200405 if (cpufreq_driver->setpolicy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
407 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700408 err = 0;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530409 } else if (!strnicmp(str_governor, "powersave",
410 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700412 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530414 } else if (has_target()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700416
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800417 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700418
419 t = __find_governor(str_governor);
420
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700421 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700422 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700423
Kees Cook1a8e1462011-05-04 08:38:56 -0700424 mutex_unlock(&cpufreq_governor_mutex);
425 ret = request_module("cpufreq_%s", str_governor);
426 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700427
Kees Cook1a8e1462011-05-04 08:38:56 -0700428 if (ret == 0)
429 t = __find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700430 }
431
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700432 if (t != NULL) {
433 *governor = t;
434 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700436
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800437 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
Dave Jones29464f22009-01-18 01:37:11 -0500439out:
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700440 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530444 * cpufreq_per_cpu_attr_read() / show_##file_name() -
445 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 *
447 * Write out information from cpufreq_driver->policy[cpu]; object must be
448 * "unsigned int".
449 */
450
Dave Jones32ee8c32006-02-28 00:43:23 -0500451#define show_one(file_name, object) \
452static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500453(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500454{ \
Dave Jones29464f22009-01-18 01:37:11 -0500455 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
458show_one(cpuinfo_min_freq, cpuinfo.min_freq);
459show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100460show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461show_one(scaling_min_freq, min);
462show_one(scaling_max_freq, max);
463show_one(scaling_cur_freq, cur);
464
Viresh Kumar037ce832013-10-02 14:13:16 +0530465static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530466 struct cpufreq_policy *new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468/**
469 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
470 */
471#define store_one(file_name, object) \
472static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500473(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{ \
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530475 int ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 struct cpufreq_policy new_policy; \
477 \
478 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
479 if (ret) \
480 return -EINVAL; \
481 \
Dave Jones29464f22009-01-18 01:37:11 -0500482 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (ret != 1) \
484 return -EINVAL; \
485 \
Viresh Kumar037ce832013-10-02 14:13:16 +0530486 ret = cpufreq_set_policy(policy, &new_policy); \
Thomas Renninger7970e082006-04-13 15:14:04 +0200487 policy->user_policy.object = policy->object; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 \
489 return ret ? ret : count; \
490}
491
Dave Jones29464f22009-01-18 01:37:11 -0500492store_one(scaling_min_freq, min);
493store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495/**
496 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
497 */
Dave Jones905d77c2008-03-05 14:28:32 -0500498static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
499 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800501 unsigned int cur_freq = __cpufreq_get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (!cur_freq)
503 return sprintf(buf, "<unknown>");
504 return sprintf(buf, "%u\n", cur_freq);
505}
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507/**
508 * show_scaling_governor - show the current policy for the specified CPU
509 */
Dave Jones905d77c2008-03-05 14:28:32 -0500510static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Dave Jones29464f22009-01-18 01:37:11 -0500512 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return sprintf(buf, "powersave\n");
514 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
515 return sprintf(buf, "performance\n");
516 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200517 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500518 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return -EINVAL;
520}
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522/**
523 * store_scaling_governor - store policy for the specified CPU
524 */
Dave Jones905d77c2008-03-05 14:28:32 -0500525static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
526 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530528 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 char str_governor[16];
530 struct cpufreq_policy new_policy;
531
532 ret = cpufreq_get_policy(&new_policy, policy->cpu);
533 if (ret)
534 return ret;
535
Dave Jones29464f22009-01-18 01:37:11 -0500536 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (ret != 1)
538 return -EINVAL;
539
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530540 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
541 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return -EINVAL;
543
Viresh Kumar037ce832013-10-02 14:13:16 +0530544 ret = cpufreq_set_policy(policy, &new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200545
546 policy->user_policy.policy = policy->policy;
547 policy->user_policy.governor = policy->governor;
Thomas Renninger7970e082006-04-13 15:14:04 +0200548
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530549 if (ret)
550 return ret;
551 else
552 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
555/**
556 * show_scaling_driver - show the cpufreq driver currently loaded
557 */
Dave Jones905d77c2008-03-05 14:28:32 -0500558static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200560 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
563/**
564 * show_scaling_available_governors - show the available CPUfreq governors
565 */
Dave Jones905d77c2008-03-05 14:28:32 -0500566static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
567 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 ssize_t i = 0;
570 struct cpufreq_governor *t;
571
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530572 if (!has_target()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 i += sprintf(buf, "performance powersave");
574 goto out;
575 }
576
577 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
Dave Jones29464f22009-01-18 01:37:11 -0500578 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
579 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200581 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500583out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 i += sprintf(&buf[i], "\n");
585 return i;
586}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700587
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800588ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 ssize_t i = 0;
591 unsigned int cpu;
592
Rusty Russell835481d2009-01-04 05:18:06 -0800593 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (i)
595 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
596 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
597 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500598 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
600 i += sprintf(&buf[i], "\n");
601 return i;
602}
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800603EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700605/**
606 * show_related_cpus - show the CPUs affected by each transition even if
607 * hw coordination is in use
608 */
609static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
610{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800611 return cpufreq_show_cpus(policy->related_cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700612}
613
614/**
615 * show_affected_cpus - show the CPUs affected by each transition
616 */
617static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
618{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800619 return cpufreq_show_cpus(policy->cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700620}
621
Venki Pallipadi9e769882007-10-26 10:18:21 -0700622static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500623 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700624{
625 unsigned int freq = 0;
626 unsigned int ret;
627
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700628 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700629 return -EINVAL;
630
631 ret = sscanf(buf, "%u", &freq);
632 if (ret != 1)
633 return -EINVAL;
634
635 policy->governor->store_setspeed(policy, freq);
636
637 return count;
638}
639
640static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
641{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700642 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700643 return sprintf(buf, "<unsupported>\n");
644
645 return policy->governor->show_setspeed(policy, buf);
646}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Thomas Renningere2f74f32009-11-19 12:31:01 +0100648/**
viresh kumar8bf1ac722012-10-23 01:23:33 +0200649 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100650 */
651static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
652{
653 unsigned int limit;
654 int ret;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200655 if (cpufreq_driver->bios_limit) {
656 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
Thomas Renningere2f74f32009-11-19 12:31:01 +0100657 if (!ret)
658 return sprintf(buf, "%u\n", limit);
659 }
660 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
661}
662
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200663cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
664cpufreq_freq_attr_ro(cpuinfo_min_freq);
665cpufreq_freq_attr_ro(cpuinfo_max_freq);
666cpufreq_freq_attr_ro(cpuinfo_transition_latency);
667cpufreq_freq_attr_ro(scaling_available_governors);
668cpufreq_freq_attr_ro(scaling_driver);
669cpufreq_freq_attr_ro(scaling_cur_freq);
670cpufreq_freq_attr_ro(bios_limit);
671cpufreq_freq_attr_ro(related_cpus);
672cpufreq_freq_attr_ro(affected_cpus);
673cpufreq_freq_attr_rw(scaling_min_freq);
674cpufreq_freq_attr_rw(scaling_max_freq);
675cpufreq_freq_attr_rw(scaling_governor);
676cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Dave Jones905d77c2008-03-05 14:28:32 -0500678static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 &cpuinfo_min_freq.attr,
680 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100681 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 &scaling_min_freq.attr,
683 &scaling_max_freq.attr,
684 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700685 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 &scaling_governor.attr,
687 &scaling_driver.attr,
688 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700689 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 NULL
691};
692
Dave Jones29464f22009-01-18 01:37:11 -0500693#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
694#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Dave Jones29464f22009-01-18 01:37:11 -0500696static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Dave Jones905d77c2008-03-05 14:28:32 -0500698 struct cpufreq_policy *policy = to_policy(kobj);
699 struct freq_attr *fattr = to_attr(attr);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530700 ssize_t ret;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530701
702 if (!down_read_trylock(&cpufreq_rwsem))
Viresh Kumar1b750e32013-10-02 14:13:09 +0530703 return -EINVAL;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800704
viresh kumarad7722d2013-10-18 19:10:15 +0530705 down_read(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800706
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530707 if (fattr->show)
708 ret = fattr->show(policy, buf);
709 else
710 ret = -EIO;
711
viresh kumarad7722d2013-10-18 19:10:15 +0530712 up_read(&policy->rwsem);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530713 up_read(&cpufreq_rwsem);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return ret;
716}
717
Dave Jones905d77c2008-03-05 14:28:32 -0500718static ssize_t store(struct kobject *kobj, struct attribute *attr,
719 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Dave Jones905d77c2008-03-05 14:28:32 -0500721 struct cpufreq_policy *policy = to_policy(kobj);
722 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500723 ssize_t ret = -EINVAL;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530724
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530725 get_online_cpus();
726
727 if (!cpu_online(policy->cpu))
728 goto unlock;
729
Viresh Kumar6eed9402013-08-06 22:53:11 +0530730 if (!down_read_trylock(&cpufreq_rwsem))
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530731 goto unlock;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800732
viresh kumarad7722d2013-10-18 19:10:15 +0530733 down_write(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800734
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530735 if (fattr->store)
736 ret = fattr->store(policy, buf, count);
737 else
738 ret = -EIO;
739
viresh kumarad7722d2013-10-18 19:10:15 +0530740 up_write(&policy->rwsem);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530741
Viresh Kumar6eed9402013-08-06 22:53:11 +0530742 up_read(&cpufreq_rwsem);
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530743unlock:
744 put_online_cpus();
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return ret;
747}
748
Dave Jones905d77c2008-03-05 14:28:32 -0500749static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Dave Jones905d77c2008-03-05 14:28:32 -0500751 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200752 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 complete(&policy->kobj_unregister);
754}
755
Emese Revfy52cf25d2010-01-19 02:58:23 +0100756static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 .show = show,
758 .store = store,
759};
760
761static struct kobj_type ktype_cpufreq = {
762 .sysfs_ops = &sysfs_ops,
763 .default_attrs = default_attrs,
764 .release = cpufreq_sysfs_release,
765};
766
Viresh Kumar2361be22013-05-17 16:09:09 +0530767struct kobject *cpufreq_global_kobject;
768EXPORT_SYMBOL(cpufreq_global_kobject);
769
770static int cpufreq_global_kobject_usage;
771
772int cpufreq_get_global_kobject(void)
773{
774 if (!cpufreq_global_kobject_usage++)
775 return kobject_add(cpufreq_global_kobject,
776 &cpu_subsys.dev_root->kobj, "%s", "cpufreq");
777
778 return 0;
779}
780EXPORT_SYMBOL(cpufreq_get_global_kobject);
781
782void cpufreq_put_global_kobject(void)
783{
784 if (!--cpufreq_global_kobject_usage)
785 kobject_del(cpufreq_global_kobject);
786}
787EXPORT_SYMBOL(cpufreq_put_global_kobject);
788
789int cpufreq_sysfs_create_file(const struct attribute *attr)
790{
791 int ret = cpufreq_get_global_kobject();
792
793 if (!ret) {
794 ret = sysfs_create_file(cpufreq_global_kobject, attr);
795 if (ret)
796 cpufreq_put_global_kobject();
797 }
798
799 return ret;
800}
801EXPORT_SYMBOL(cpufreq_sysfs_create_file);
802
803void cpufreq_sysfs_remove_file(const struct attribute *attr)
804{
805 sysfs_remove_file(cpufreq_global_kobject, attr);
806 cpufreq_put_global_kobject();
807}
808EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
809
Dave Jones19d6f7e2009-07-08 17:35:39 -0400810/* symlink affected CPUs */
Viresh Kumar308b60e2013-07-31 14:35:14 +0200811static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400812{
813 unsigned int j;
814 int ret = 0;
815
816 for_each_cpu(j, policy->cpus) {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800817 struct device *cpu_dev;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400818
Viresh Kumar308b60e2013-07-31 14:35:14 +0200819 if (j == policy->cpu)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400820 continue;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400821
Viresh Kumare8fdde12013-07-31 14:31:33 +0200822 pr_debug("Adding link for CPU: %u\n", j);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800823 cpu_dev = get_cpu_device(j);
824 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Dave Jones19d6f7e2009-07-08 17:35:39 -0400825 "cpufreq");
Rafael J. Wysocki71c34612013-08-04 01:19:34 +0200826 if (ret)
827 break;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400828 }
829 return ret;
830}
831
Viresh Kumar308b60e2013-07-31 14:35:14 +0200832static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800833 struct device *dev)
Dave Jones909a6942009-07-08 18:05:42 -0400834{
835 struct freq_attr **drv_attr;
Dave Jones909a6942009-07-08 18:05:42 -0400836 int ret = 0;
Dave Jones909a6942009-07-08 18:05:42 -0400837
838 /* prepare interface data */
839 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800840 &dev->kobj, "cpufreq");
Dave Jones909a6942009-07-08 18:05:42 -0400841 if (ret)
842 return ret;
843
844 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200845 drv_attr = cpufreq_driver->attr;
Dave Jones909a6942009-07-08 18:05:42 -0400846 while ((drv_attr) && (*drv_attr)) {
847 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
848 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200849 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400850 drv_attr++;
851 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200852 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -0400853 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
854 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200855 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400856 }
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530857 if (has_target()) {
Dave Jones909a6942009-07-08 18:05:42 -0400858 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
859 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200860 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400861 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200862 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +0100863 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
864 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200865 goto err_out_kobj_put;
Thomas Renningere2f74f32009-11-19 12:31:01 +0100866 }
Dave Jones909a6942009-07-08 18:05:42 -0400867
Viresh Kumar308b60e2013-07-31 14:35:14 +0200868 ret = cpufreq_add_dev_symlink(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400869 if (ret)
870 goto err_out_kobj_put;
871
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530872 return ret;
873
874err_out_kobj_put:
875 kobject_put(&policy->kobj);
876 wait_for_completion(&policy->kobj_unregister);
877 return ret;
878}
879
880static void cpufreq_init_policy(struct cpufreq_policy *policy)
881{
882 struct cpufreq_policy new_policy;
883 int ret = 0;
884
Viresh Kumard5b73cd2013-08-06 22:53:06 +0530885 memcpy(&new_policy, policy, sizeof(*policy));
Jason Barona27a9ab2013-12-19 22:50:50 +0000886
887 /* Use the default policy if its valid. */
888 if (cpufreq_driver->setpolicy)
889 cpufreq_parse_governor(policy->governor->name,
890 &new_policy.policy, NULL);
891
Viresh Kumar037ce832013-10-02 14:13:16 +0530892 /* assure that the starting sequence is run in cpufreq_set_policy */
Dave Jonesecf7e462009-07-08 18:48:47 -0400893 policy->governor = NULL;
894
895 /* set default policy */
Viresh Kumar037ce832013-10-02 14:13:16 +0530896 ret = cpufreq_set_policy(policy, &new_policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400897 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200898 pr_debug("setting policy failed\n");
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200899 if (cpufreq_driver->exit)
900 cpufreq_driver->exit(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400901 }
Dave Jones909a6942009-07-08 18:05:42 -0400902}
903
Viresh Kumarfcf80582013-01-29 14:39:08 +0000904#ifdef CONFIG_HOTPLUG_CPU
Viresh Kumard8d3b472013-08-04 01:20:07 +0200905static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,
Viresh Kumar42f921a2013-12-20 21:26:02 +0530906 unsigned int cpu, struct device *dev)
Viresh Kumarfcf80582013-01-29 14:39:08 +0000907{
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530908 int ret = 0;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000909 unsigned long flags;
910
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530911 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +0530912 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
913 if (ret) {
914 pr_err("%s: Failed to stop governor\n", __func__);
915 return ret;
916 }
917 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000918
viresh kumarad7722d2013-10-18 19:10:15 +0530919 down_write(&policy->rwsem);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530920
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000921 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530922
Viresh Kumarfcf80582013-01-29 14:39:08 +0000923 cpumask_set_cpu(cpu, policy->cpus);
924 per_cpu(cpufreq_cpu_data, cpu) = policy;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000925 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000926
viresh kumarad7722d2013-10-18 19:10:15 +0530927 up_write(&policy->rwsem);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530928
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530929 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +0530930 if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
931 (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
932 pr_err("%s: Failed to start governor\n", __func__);
933 return ret;
934 }
Viresh Kumar820c6ca2013-04-22 00:48:03 +0200935 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000936
Viresh Kumar42f921a2013-12-20 21:26:02 +0530937 return sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
Viresh Kumarfcf80582013-01-29 14:39:08 +0000938}
939#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530941static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu)
942{
943 struct cpufreq_policy *policy;
944 unsigned long flags;
945
Lan Tianyu44871c92013-09-11 15:05:05 +0800946 read_lock_irqsave(&cpufreq_driver_lock, flags);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530947
948 policy = per_cpu(cpufreq_cpu_data_fallback, cpu);
949
Lan Tianyu44871c92013-09-11 15:05:05 +0800950 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530951
952 return policy;
953}
954
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +0530955static struct cpufreq_policy *cpufreq_policy_alloc(void)
956{
957 struct cpufreq_policy *policy;
958
959 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
960 if (!policy)
961 return NULL;
962
963 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
964 goto err_free_policy;
965
966 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
967 goto err_free_cpumask;
968
Lukasz Majewskic88a1f82013-08-06 22:53:08 +0530969 INIT_LIST_HEAD(&policy->policy_list);
viresh kumarad7722d2013-10-18 19:10:15 +0530970 init_rwsem(&policy->rwsem);
971
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +0530972 return policy;
973
974err_free_cpumask:
975 free_cpumask_var(policy->cpus);
976err_free_policy:
977 kfree(policy);
978
979 return NULL;
980}
981
Viresh Kumar42f921a2013-12-20 21:26:02 +0530982static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
983{
984 struct kobject *kobj;
985 struct completion *cmp;
986
Viresh Kumarfcd7af92014-01-07 07:10:10 +0530987 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
988 CPUFREQ_REMOVE_POLICY, policy);
989
Viresh Kumar42f921a2013-12-20 21:26:02 +0530990 down_read(&policy->rwsem);
991 kobj = &policy->kobj;
992 cmp = &policy->kobj_unregister;
993 up_read(&policy->rwsem);
994 kobject_put(kobj);
995
996 /*
997 * We need to make sure that the underlying kobj is
998 * actually not referenced anymore by anybody before we
999 * proceed with unloading.
1000 */
1001 pr_debug("waiting for dropping of refcount\n");
1002 wait_for_completion(cmp);
1003 pr_debug("wait complete\n");
1004}
1005
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301006static void cpufreq_policy_free(struct cpufreq_policy *policy)
1007{
1008 free_cpumask_var(policy->related_cpus);
1009 free_cpumask_var(policy->cpus);
1010 kfree(policy);
1011}
1012
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301013static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
1014{
Srivatsa S. Bhat99ec8992013-09-12 17:29:09 +05301015 if (WARN_ON(cpu == policy->cpu))
Srivatsa S. Bhatcb38ed52013-09-12 01:43:42 +05301016 return;
1017
viresh kumarad7722d2013-10-18 19:10:15 +05301018 down_write(&policy->rwsem);
Viresh Kumar8efd5762013-09-17 10:22:11 +05301019
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301020 policy->last_cpu = policy->cpu;
1021 policy->cpu = cpu;
1022
viresh kumarad7722d2013-10-18 19:10:15 +05301023 up_write(&policy->rwsem);
Viresh Kumar8efd5762013-09-17 10:22:11 +05301024
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301025 cpufreq_frequency_table_update_policy_cpu(policy);
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301026 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1027 CPUFREQ_UPDATE_POLICY_CPU, policy);
1028}
1029
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301030static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif,
1031 bool frozen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
Viresh Kumarfcf80582013-01-29 14:39:08 +00001033 unsigned int j, cpu = dev->id;
Viresh Kumar65922462013-02-07 10:56:03 +05301034 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 unsigned long flags;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001037#ifdef CONFIG_HOTPLUG_CPU
Viresh Kumar1b274292013-08-20 12:08:26 +05301038 struct cpufreq_policy *tpolicy;
Viresh Kumarfcf80582013-01-29 14:39:08 +00001039 struct cpufreq_governor *gov;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001040#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Ashok Rajc32b6b82005-10-30 14:59:54 -08001042 if (cpu_is_offline(cpu))
1043 return 0;
1044
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001045 pr_debug("adding CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047#ifdef CONFIG_SMP
1048 /* check whether a different CPU already registered this
1049 * CPU because it is in the same boat. */
1050 policy = cpufreq_cpu_get(cpu);
1051 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -05001052 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 return 0;
1054 }
Li Zhong5025d622013-08-21 01:31:08 +02001055#endif
Viresh Kumarfcf80582013-01-29 14:39:08 +00001056
Viresh Kumar6eed9402013-08-06 22:53:11 +05301057 if (!down_read_trylock(&cpufreq_rwsem))
1058 return 0;
1059
Viresh Kumarfcf80582013-01-29 14:39:08 +00001060#ifdef CONFIG_HOTPLUG_CPU
1061 /* Check if this cpu was hot-unplugged earlier and has siblings */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001062 read_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar1b274292013-08-20 12:08:26 +05301063 list_for_each_entry(tpolicy, &cpufreq_policy_list, policy_list) {
1064 if (cpumask_test_cpu(cpu, tpolicy->related_cpus)) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001065 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301066 ret = cpufreq_add_policy_cpu(tpolicy, cpu, dev);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301067 up_read(&cpufreq_rwsem);
1068 return ret;
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301069 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001070 }
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001071 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +00001072#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001074 /*
1075 * Restore the saved policy when doing light-weight init and fall back
1076 * to the full init if that fails.
1077 */
1078 policy = frozen ? cpufreq_policy_restore(cpu) : NULL;
1079 if (!policy) {
1080 frozen = false;
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301081 policy = cpufreq_policy_alloc();
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001082 if (!policy)
1083 goto nomem_out;
1084 }
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301085
1086 /*
1087 * In the resume path, since we restore a saved policy, the assignment
1088 * to policy->cpu is like an update of the existing policy, rather than
1089 * the creation of a brand new one. So we need to perform this update
1090 * by invoking update_policy_cpu().
1091 */
1092 if (frozen && cpu != policy->cpu)
1093 update_policy_cpu(policy, cpu);
1094 else
1095 policy->cpu = cpu;
1096
Viresh Kumar65922462013-02-07 10:56:03 +05301097 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Rusty Russell835481d2009-01-04 05:18:06 -08001098 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 init_completion(&policy->kobj_unregister);
David Howells65f27f32006-11-22 14:55:48 +00001101 INIT_WORK(&policy->update, handle_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 /* call driver. From then on the cpufreq must be able
1104 * to accept all calls to ->verify and ->setpolicy for this CPU
1105 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001106 ret = cpufreq_driver->init(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001108 pr_debug("initialization failed\n");
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301109 goto err_set_policy_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 }
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001111
Viresh Kumar652ed952014-01-09 20:38:43 +05301112 write_lock_irqsave(&cpufreq_driver_lock, flags);
1113 for_each_cpu(j, policy->cpus)
1114 per_cpu(cpufreq_cpu_data, j) = policy;
1115 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1116
Viresh Kumarda60ce92013-10-03 20:28:30 +05301117 if (cpufreq_driver->get) {
1118 policy->cur = cpufreq_driver->get(policy->cpu);
1119 if (!policy->cur) {
1120 pr_err("%s: ->get() failed\n", __func__);
1121 goto err_get_freq;
1122 }
1123 }
1124
Viresh Kumard3916692013-12-03 11:20:46 +05301125 /*
1126 * Sometimes boot loaders set CPU frequency to a value outside of
1127 * frequency table present with cpufreq core. In such cases CPU might be
1128 * unstable if it has to run on that frequency for long duration of time
1129 * and so its better to set it to a frequency which is specified in
1130 * freq-table. This also makes cpufreq stats inconsistent as
1131 * cpufreq-stats would fail to register because current frequency of CPU
1132 * isn't found in freq-table.
1133 *
1134 * Because we don't want this change to effect boot process badly, we go
1135 * for the next freq which is >= policy->cur ('cur' must be set by now,
1136 * otherwise we will end up setting freq to lowest of the table as 'cur'
1137 * is initialized to zero).
1138 *
1139 * We are passing target-freq as "policy->cur - 1" otherwise
1140 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1141 * equal to target-freq.
1142 */
1143 if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1144 && has_target()) {
1145 /* Are we running at unknown frequency ? */
1146 ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1147 if (ret == -EINVAL) {
1148 /* Warn user and fix it */
1149 pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1150 __func__, policy->cpu, policy->cur);
1151 ret = __cpufreq_driver_target(policy, policy->cur - 1,
1152 CPUFREQ_RELATION_L);
1153
1154 /*
1155 * Reaching here after boot in a few seconds may not
1156 * mean that system will remain stable at "unknown"
1157 * frequency for longer duration. Hence, a BUG_ON().
1158 */
1159 BUG_ON(ret);
1160 pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1161 __func__, policy->cpu, policy->cur);
1162 }
1163 }
1164
Viresh Kumarfcf80582013-01-29 14:39:08 +00001165 /* related cpus should atleast have policy->cpus */
1166 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
1167
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001168 /*
1169 * affected cpus must always be the one, which are online. We aren't
1170 * managing offline cpus here.
1171 */
1172 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1173
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301174 if (!frozen) {
1175 policy->user_policy.min = policy->min;
1176 policy->user_policy.max = policy->max;
1177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Thomas Renningera1531ac2008-07-29 22:32:58 -07001179 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1180 CPUFREQ_START, policy);
1181
Viresh Kumarfcf80582013-01-29 14:39:08 +00001182#ifdef CONFIG_HOTPLUG_CPU
1183 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
1184 if (gov) {
1185 policy->governor = gov;
1186 pr_debug("Restoring governor %s for cpu %d\n",
1187 policy->governor->name, cpu);
Thomas Renninger4bfa0422009-07-24 15:25:03 +02001188 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001189#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301191 if (!frozen) {
Viresh Kumar308b60e2013-07-31 14:35:14 +02001192 ret = cpufreq_add_dev_interface(policy, dev);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301193 if (ret)
1194 goto err_out_unregister;
Viresh Kumarfcd7af92014-01-07 07:10:10 +05301195 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1196 CPUFREQ_CREATE_POLICY, policy);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301197 }
Dave Jones8ff69732006-03-05 03:37:23 -05001198
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301199 write_lock_irqsave(&cpufreq_driver_lock, flags);
1200 list_add(&policy->policy_list, &cpufreq_policy_list);
1201 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1202
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301203 cpufreq_init_policy(policy);
1204
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301205 if (!frozen) {
1206 policy->user_policy.policy = policy->policy;
1207 policy->user_policy.governor = policy->governor;
1208 }
1209
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001210 kobject_uevent(&policy->kobj, KOBJ_ADD);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301211 up_read(&cpufreq_rwsem);
1212
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001213 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 return 0;
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217err_out_unregister:
Viresh Kumar652ed952014-01-09 20:38:43 +05301218err_get_freq:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001219 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar474deff2013-08-20 12:08:25 +05301220 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -07001221 per_cpu(cpufreq_cpu_data, j) = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001222 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Viresh Kumarda60ce92013-10-03 20:28:30 +05301224 if (cpufreq_driver->exit)
1225 cpufreq_driver->exit(policy);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301226err_set_policy_cpu:
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001227 if (frozen) {
1228 /* Do not leave stale fallback data behind. */
1229 per_cpu(cpufreq_cpu_data_fallback, cpu) = NULL;
Viresh Kumar42f921a2013-12-20 21:26:02 +05301230 cpufreq_policy_put_kobj(policy);
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001231 }
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301232 cpufreq_policy_free(policy);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234nomem_out:
Viresh Kumar6eed9402013-08-06 22:53:11 +05301235 up_read(&cpufreq_rwsem);
1236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return ret;
1238}
1239
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301240/**
1241 * cpufreq_add_dev - add a CPU device
1242 *
1243 * Adds the cpufreq interface for a CPU device.
1244 *
1245 * The Oracle says: try running cpufreq registration/unregistration concurrently
1246 * with with cpu hotplugging and all hell will break loose. Tried to clean this
1247 * mess up, but more thorough testing is needed. - Mathieu
1248 */
1249static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1250{
1251 return __cpufreq_add_dev(dev, sif, false);
1252}
1253
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301254static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy *policy,
Viresh Kumar42f921a2013-12-20 21:26:02 +05301255 unsigned int old_cpu)
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301256{
1257 struct device *cpu_dev;
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301258 int ret;
1259
1260 /* first sibling now owns the new sysfs dir */
Viresh Kumar9c8f1ee2013-09-12 17:06:33 +05301261 cpu_dev = get_cpu_device(cpumask_any_but(policy->cpus, old_cpu));
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301262
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301263 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301264 ret = kobject_move(&policy->kobj, &cpu_dev->kobj);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301265 if (ret) {
1266 pr_err("%s: Failed to move kobj: %d", __func__, ret);
1267
viresh kumarad7722d2013-10-18 19:10:15 +05301268 down_write(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301269 cpumask_set_cpu(old_cpu, policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301270 up_write(&policy->rwsem);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301271
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301272 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301273 "cpufreq");
1274
1275 return -EINVAL;
1276 }
1277
1278 return cpu_dev->id;
1279}
1280
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301281static int __cpufreq_remove_dev_prepare(struct device *dev,
1282 struct subsys_interface *sif,
1283 bool frozen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301285 unsigned int cpu = dev->id, cpus;
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301286 int new_cpu, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 unsigned long flags;
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301288 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001290 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001292 write_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301294 policy = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301296 /* Save the policy somewhere when doing a light-weight tear-down */
1297 if (frozen)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301298 per_cpu(cpufreq_cpu_data_fallback, cpu) = policy;
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301299
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001300 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301302 if (!policy) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001303 pr_debug("%s: No cpu_data found\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301307 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301308 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1309 if (ret) {
1310 pr_err("%s: Failed to stop governor\n", __func__);
1311 return ret;
1312 }
1313 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001314
Jacob Shin27ecddc2011-04-27 13:32:11 -05001315#ifdef CONFIG_HOTPLUG_CPU
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001316 if (!cpufreq_driver->setpolicy)
Dirk Brandewiefa69e332013-02-06 09:02:11 -08001317 strncpy(per_cpu(cpufreq_cpu_governor, cpu),
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301318 policy->governor->name, CPUFREQ_NAME_LEN);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001319#endif
1320
viresh kumarad7722d2013-10-18 19:10:15 +05301321 down_read(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301322 cpus = cpumask_weight(policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301323 up_read(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Srivatsa S. Bhat61173f22013-09-12 01:43:25 +05301325 if (cpu != policy->cpu) {
1326 if (!frozen)
1327 sysfs_remove_link(&dev->kobj, "cpufreq");
Viresh Kumar73bf0fc2013-02-05 22:21:14 +01001328 } else if (cpus > 1) {
Viresh Kumar42f921a2013-12-20 21:26:02 +05301329 new_cpu = cpufreq_nominate_new_policy_cpu(policy, cpu);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301330 if (new_cpu >= 0) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301331 update_policy_cpu(policy, new_cpu);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301332
1333 if (!frozen) {
Viresh Kumar75949c92013-10-02 14:13:13 +05301334 pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
1335 __func__, new_cpu, cpu);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301336 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001337 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001338 }
Venki Pallipadiec282972007-03-26 12:03:19 -07001339
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301340 return 0;
1341}
1342
1343static int __cpufreq_remove_dev_finish(struct device *dev,
1344 struct subsys_interface *sif,
1345 bool frozen)
1346{
1347 unsigned int cpu = dev->id, cpus;
1348 int ret;
1349 unsigned long flags;
1350 struct cpufreq_policy *policy;
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301351
1352 read_lock_irqsave(&cpufreq_driver_lock, flags);
1353 policy = per_cpu(cpufreq_cpu_data, cpu);
1354 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1355
1356 if (!policy) {
1357 pr_debug("%s: No cpu_data found\n", __func__);
1358 return -EINVAL;
1359 }
1360
viresh kumarad7722d2013-10-18 19:10:15 +05301361 down_write(&policy->rwsem);
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301362 cpus = cpumask_weight(policy->cpus);
Viresh Kumar9c8f1ee2013-09-12 17:06:33 +05301363
1364 if (cpus > 1)
1365 cpumask_clear_cpu(cpu, policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301366 up_write(&policy->rwsem);
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301367
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001368 /* If cpu is last user of policy, free policy */
1369 if (cpus == 1) {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301370 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301371 ret = __cpufreq_governor(policy,
1372 CPUFREQ_GOV_POLICY_EXIT);
1373 if (ret) {
1374 pr_err("%s: Failed to exit governor\n",
1375 __func__);
1376 return ret;
1377 }
Viresh Kumaredab2fb2013-08-20 12:08:22 +05301378 }
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001379
Viresh Kumar42f921a2013-12-20 21:26:02 +05301380 if (!frozen)
1381 cpufreq_policy_put_kobj(policy);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301382
1383 /*
1384 * Perform the ->exit() even during light-weight tear-down,
1385 * since this is a core component, and is essential for the
1386 * subsequent light-weight ->init() to succeed.
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001387 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001388 if (cpufreq_driver->exit)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301389 cpufreq_driver->exit(policy);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001390
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301391 /* Remove policy from list of active policies */
1392 write_lock_irqsave(&cpufreq_driver_lock, flags);
1393 list_del(&policy->policy_list);
1394 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1395
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301396 if (!frozen)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301397 cpufreq_policy_free(policy);
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001398 } else {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301399 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301400 if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
1401 (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
1402 pr_err("%s: Failed to start governor\n",
1403 __func__);
1404 return ret;
1405 }
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001406 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Viresh Kumar474deff2013-08-20 12:08:25 +05301409 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return 0;
1411}
1412
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301413/**
Viresh Kumar27a862e2013-10-02 14:13:14 +05301414 * cpufreq_remove_dev - remove a CPU device
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301415 *
1416 * Removes the cpufreq interface for a CPU device.
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301417 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001418static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001419{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001420 unsigned int cpu = dev->id;
Viresh Kumar27a862e2013-10-02 14:13:14 +05301421 int ret;
Venki Pallipadiec282972007-03-26 12:03:19 -07001422
1423 if (cpu_is_offline(cpu))
1424 return 0;
1425
Viresh Kumar27a862e2013-10-02 14:13:14 +05301426 ret = __cpufreq_remove_dev_prepare(dev, sif, false);
1427
1428 if (!ret)
1429 ret = __cpufreq_remove_dev_finish(dev, sif, false);
1430
1431 return ret;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001432}
1433
David Howells65f27f32006-11-22 14:55:48 +00001434static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
David Howells65f27f32006-11-22 14:55:48 +00001436 struct cpufreq_policy *policy =
1437 container_of(work, struct cpufreq_policy, update);
1438 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001439 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 cpufreq_update_policy(cpu);
1441}
1442
1443/**
Viresh Kumarbb176f72013-06-19 14:19:33 +05301444 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1445 * in deep trouble.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 * @cpu: cpu number
1447 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1448 * @new_freq: CPU frequency the CPU actually runs at
1449 *
Dave Jones29464f22009-01-18 01:37:11 -05001450 * We adjust to current frequency first, and need to clean up later.
1451 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301453static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1454 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301456 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301458 unsigned long flags;
1459
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001460 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 freqs.old = old_freq;
1464 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301465
1466 read_lock_irqsave(&cpufreq_driver_lock, flags);
1467 policy = per_cpu(cpufreq_cpu_data, cpu);
1468 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1469
1470 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1471 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472}
1473
Dave Jones32ee8c32006-02-28 00:43:23 -05001474/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301475 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001476 * @cpu: CPU number
1477 *
1478 * This is the last known freq, without actually getting it from the driver.
1479 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1480 */
1481unsigned int cpufreq_quick_get(unsigned int cpu)
1482{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001483 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301484 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001485
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001486 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1487 return cpufreq_driver->get(cpu);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001488
1489 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001490 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301491 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001492 cpufreq_cpu_put(policy);
1493 }
1494
Dave Jones4d34a672008-02-07 16:33:49 -05001495 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001496}
1497EXPORT_SYMBOL(cpufreq_quick_get);
1498
Jesse Barnes3d737102011-06-28 10:59:12 -07001499/**
1500 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1501 * @cpu: CPU number
1502 *
1503 * Just return the max possible frequency for a given CPU.
1504 */
1505unsigned int cpufreq_quick_get_max(unsigned int cpu)
1506{
1507 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1508 unsigned int ret_freq = 0;
1509
1510 if (policy) {
1511 ret_freq = policy->max;
1512 cpufreq_cpu_put(policy);
1513 }
1514
1515 return ret_freq;
1516}
1517EXPORT_SYMBOL(cpufreq_quick_get_max);
1518
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001519static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001521 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301522 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001524 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001525 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001527 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301529 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001530 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301531 /* verify no discrepancy between actual and
1532 saved value exists */
1533 if (unlikely(ret_freq != policy->cur)) {
1534 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 schedule_work(&policy->update);
1536 }
1537 }
1538
Dave Jones4d34a672008-02-07 16:33:49 -05001539 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001540}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001542/**
1543 * cpufreq_get - get the current CPU frequency (in kHz)
1544 * @cpu: CPU number
1545 *
1546 * Get the CPU current (static) CPU frequency
1547 */
1548unsigned int cpufreq_get(unsigned int cpu)
1549{
viresh kumarad7722d2013-10-18 19:10:15 +05301550 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001551 unsigned int ret_freq = 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001552
Viresh Kumar26ca8692013-09-20 22:37:31 +05301553 if (cpufreq_disabled() || !cpufreq_driver)
1554 return -ENOENT;
1555
viresh kumarad7722d2013-10-18 19:10:15 +05301556 BUG_ON(!policy);
1557
Viresh Kumar6eed9402013-08-06 22:53:11 +05301558 if (!down_read_trylock(&cpufreq_rwsem))
1559 return 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001560
viresh kumarad7722d2013-10-18 19:10:15 +05301561 down_read(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001562
1563 ret_freq = __cpufreq_get(cpu);
1564
viresh kumarad7722d2013-10-18 19:10:15 +05301565 up_read(&policy->rwsem);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301566 up_read(&cpufreq_rwsem);
1567
Dave Jones4d34a672008-02-07 16:33:49 -05001568 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569}
1570EXPORT_SYMBOL(cpufreq_get);
1571
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001572static struct subsys_interface cpufreq_interface = {
1573 .name = "cpufreq",
1574 .subsys = &cpu_subsys,
1575 .add_dev = cpufreq_add_dev,
1576 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001577};
1578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001580 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1581 *
1582 * This function is only executed for the boot processor. The other CPUs
1583 * have been put offline by means of CPU hotplug.
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001584 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001585static int cpufreq_bp_suspend(void)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001586{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301587 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001588
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001589 int cpu = smp_processor_id();
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301590 struct cpufreq_policy *policy;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001591
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001592 pr_debug("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001593
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001594 /* If there's no policy for the boot CPU, we have nothing to do. */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301595 policy = cpufreq_cpu_get(cpu);
1596 if (!policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001597 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001598
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001599 if (cpufreq_driver->suspend) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301600 ret = cpufreq_driver->suspend(policy);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001601 if (ret)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001602 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301603 "step on CPU %u\n", policy->cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001604 }
1605
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301606 cpufreq_cpu_put(policy);
Dave Jonesc9060492008-02-07 16:32:18 -05001607 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001608}
1609
1610/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001611 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 *
1613 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001614 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1615 * restored. It will verify that the current freq is in sync with
1616 * what we believe it to be. This is a bit later than when it
1617 * should be, but nonethteless it's better than calling
1618 * cpufreq_driver->get() here which might re-enable interrupts...
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001619 *
1620 * This function is only executed for the boot CPU. The other CPUs have not
1621 * been turned on yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001623static void cpufreq_bp_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301625 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001626
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001627 int cpu = smp_processor_id();
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301628 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001630 pr_debug("resuming cpu %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001632 /* If there's no policy for the boot CPU, we have nothing to do. */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301633 policy = cpufreq_cpu_get(cpu);
1634 if (!policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001635 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001637 if (cpufreq_driver->resume) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301638 ret = cpufreq_driver->resume(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 if (ret) {
1640 printk(KERN_ERR "cpufreq: resume failed in ->resume "
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301641 "step on CPU %u\n", policy->cpu);
Dave Jonesc9060492008-02-07 16:32:18 -05001642 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
1644 }
1645
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301646 schedule_work(&policy->update);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001647
Dave Jonesc9060492008-02-07 16:32:18 -05001648fail:
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301649 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650}
1651
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001652static struct syscore_ops cpufreq_syscore_ops = {
1653 .suspend = cpufreq_bp_suspend,
1654 .resume = cpufreq_bp_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655};
1656
Borislav Petkov9d950462013-01-20 10:24:28 +00001657/**
1658 * cpufreq_get_current_driver - return current driver's name
1659 *
1660 * Return the name string of the currently loaded cpufreq driver
1661 * or NULL, if none.
1662 */
1663const char *cpufreq_get_current_driver(void)
1664{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001665 if (cpufreq_driver)
1666 return cpufreq_driver->name;
1667
1668 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001669}
1670EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
1672/*********************************************************************
1673 * NOTIFIER LISTS INTERFACE *
1674 *********************************************************************/
1675
1676/**
1677 * cpufreq_register_notifier - register a driver with cpufreq
1678 * @nb: notifier function to register
1679 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1680 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001681 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 * are notified about clock rate changes (once before and once after
1683 * the transition), or a list of drivers that are notified about
1684 * changes in cpufreq policy.
1685 *
1686 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001687 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 */
1689int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1690{
1691 int ret;
1692
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001693 if (cpufreq_disabled())
1694 return -EINVAL;
1695
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001696 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1697
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 switch (list) {
1699 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001700 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001701 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 break;
1703 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001704 ret = blocking_notifier_chain_register(
1705 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 break;
1707 default:
1708 ret = -EINVAL;
1709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
1711 return ret;
1712}
1713EXPORT_SYMBOL(cpufreq_register_notifier);
1714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715/**
1716 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1717 * @nb: notifier block to be unregistered
Viresh Kumarbb176f72013-06-19 14:19:33 +05301718 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 *
1720 * Remove a driver from the CPU frequency notifier list.
1721 *
1722 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001723 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 */
1725int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1726{
1727 int ret;
1728
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001729 if (cpufreq_disabled())
1730 return -EINVAL;
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 switch (list) {
1733 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001734 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001735 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 break;
1737 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001738 ret = blocking_notifier_chain_unregister(
1739 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 break;
1741 default:
1742 ret = -EINVAL;
1743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 return ret;
1746}
1747EXPORT_SYMBOL(cpufreq_unregister_notifier);
1748
1749
1750/*********************************************************************
1751 * GOVERNORS *
1752 *********************************************************************/
1753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754int __cpufreq_driver_target(struct cpufreq_policy *policy,
1755 unsigned int target_freq,
1756 unsigned int relation)
1757{
1758 int retval = -EINVAL;
Viresh Kumar72499242012-10-31 01:28:21 +01001759 unsigned int old_target_freq = target_freq;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001760
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001761 if (cpufreq_disabled())
1762 return -ENODEV;
1763
Viresh Kumar72499242012-10-31 01:28:21 +01001764 /* Make sure that target_freq is within supported range */
1765 if (target_freq > policy->max)
1766 target_freq = policy->max;
1767 if (target_freq < policy->min)
1768 target_freq = policy->min;
1769
1770 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1771 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001772
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301773 /*
1774 * This might look like a redundant call as we are checking it again
1775 * after finding index. But it is left intentionally for cases where
1776 * exactly same freq is called again and so we can save on few function
1777 * calls.
1778 */
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001779 if (target_freq == policy->cur)
1780 return 0;
1781
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001782 if (cpufreq_driver->target)
1783 retval = cpufreq_driver->target(policy, target_freq, relation);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301784 else if (cpufreq_driver->target_index) {
1785 struct cpufreq_frequency_table *freq_table;
Viresh Kumard4019f02013-08-14 19:38:24 +05301786 struct cpufreq_freqs freqs;
1787 bool notify;
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301788 int index;
Ashok Raj90d45d12005-11-08 21:34:24 -08001789
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301790 freq_table = cpufreq_frequency_get_table(policy->cpu);
1791 if (unlikely(!freq_table)) {
1792 pr_err("%s: Unable to find freq_table\n", __func__);
1793 goto out;
1794 }
1795
1796 retval = cpufreq_frequency_table_target(policy, freq_table,
1797 target_freq, relation, &index);
1798 if (unlikely(retval)) {
1799 pr_err("%s: Unable to find matching freq\n", __func__);
1800 goto out;
1801 }
1802
Viresh Kumard4019f02013-08-14 19:38:24 +05301803 if (freq_table[index].frequency == policy->cur) {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301804 retval = 0;
Viresh Kumard4019f02013-08-14 19:38:24 +05301805 goto out;
1806 }
1807
1808 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
1809
1810 if (notify) {
1811 freqs.old = policy->cur;
1812 freqs.new = freq_table[index].frequency;
1813 freqs.flags = 0;
1814
1815 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1816 __func__, policy->cpu, freqs.old,
1817 freqs.new);
1818
1819 cpufreq_notify_transition(policy, &freqs,
1820 CPUFREQ_PRECHANGE);
1821 }
1822
1823 retval = cpufreq_driver->target_index(policy, index);
1824 if (retval)
1825 pr_err("%s: Failed to change cpu frequency: %d\n",
1826 __func__, retval);
1827
Viresh Kumarab1b1c42013-12-02 11:04:13 +05301828 if (notify)
1829 cpufreq_notify_post_transition(policy, &freqs, retval);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301830 }
1831
1832out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 return retval;
1834}
1835EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837int cpufreq_driver_target(struct cpufreq_policy *policy,
1838 unsigned int target_freq,
1839 unsigned int relation)
1840{
Julia Lawallf1829e42008-07-25 22:44:53 +02001841 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
viresh kumarad7722d2013-10-18 19:10:15 +05301843 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
1845 ret = __cpufreq_driver_target(policy, target_freq, relation);
1846
viresh kumarad7722d2013-10-18 19:10:15 +05301847 up_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 return ret;
1850}
1851EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1852
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001853/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001854 * when "event" is CPUFREQ_GOV_LIMITS
1855 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301857static int __cpufreq_governor(struct cpufreq_policy *policy,
1858 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
Dave Jonescc993ca2005-07-28 09:43:56 -07001860 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001861
1862 /* Only must be defined when default governor is known to have latency
1863 restrictions, like e.g. conservative or ondemand.
1864 That this is the case is already ensured in Kconfig
1865 */
1866#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1867 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1868#else
1869 struct cpufreq_governor *gov = NULL;
1870#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001871
1872 if (policy->governor->max_transition_latency &&
1873 policy->cpuinfo.transition_latency >
1874 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07001875 if (!gov)
1876 return -EINVAL;
1877 else {
1878 printk(KERN_WARNING "%s governor failed, too long"
1879 " transition latency of HW, fallback"
1880 " to %s governor\n",
1881 policy->governor->name,
1882 gov->name);
1883 policy->governor = gov;
1884 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Viresh Kumarfe492f32013-08-06 22:53:10 +05301887 if (event == CPUFREQ_GOV_POLICY_INIT)
1888 if (!try_module_get(policy->governor->owner))
1889 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001891 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301892 policy->cpu, event);
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001893
1894 mutex_lock(&cpufreq_governor_lock);
Srivatsa S. Bhat56d07db2013-09-07 01:23:55 +05301895 if ((policy->governor_enabled && event == CPUFREQ_GOV_START)
Viresh Kumarf73d3932013-08-31 17:53:40 +05301896 || (!policy->governor_enabled
1897 && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) {
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001898 mutex_unlock(&cpufreq_governor_lock);
1899 return -EBUSY;
1900 }
1901
1902 if (event == CPUFREQ_GOV_STOP)
1903 policy->governor_enabled = false;
1904 else if (event == CPUFREQ_GOV_START)
1905 policy->governor_enabled = true;
1906
1907 mutex_unlock(&cpufreq_governor_lock);
1908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 ret = policy->governor->governor(policy, event);
1910
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001911 if (!ret) {
1912 if (event == CPUFREQ_GOV_POLICY_INIT)
1913 policy->governor->initialized++;
1914 else if (event == CPUFREQ_GOV_POLICY_EXIT)
1915 policy->governor->initialized--;
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001916 } else {
1917 /* Restore original values */
1918 mutex_lock(&cpufreq_governor_lock);
1919 if (event == CPUFREQ_GOV_STOP)
1920 policy->governor_enabled = true;
1921 else if (event == CPUFREQ_GOV_START)
1922 policy->governor_enabled = false;
1923 mutex_unlock(&cpufreq_governor_lock);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001924 }
Viresh Kumarb3940582013-02-01 05:42:58 +00001925
Viresh Kumarfe492f32013-08-06 22:53:10 +05301926 if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
1927 ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 module_put(policy->governor->owner);
1929
1930 return ret;
1931}
1932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933int cpufreq_register_governor(struct cpufreq_governor *governor)
1934{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001935 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
1937 if (!governor)
1938 return -EINVAL;
1939
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001940 if (cpufreq_disabled())
1941 return -ENODEV;
1942
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001943 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001944
Viresh Kumarb3940582013-02-01 05:42:58 +00001945 governor->initialized = 0;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001946 err = -EBUSY;
1947 if (__find_governor(governor->name) == NULL) {
1948 err = 0;
1949 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
Dave Jones32ee8c32006-02-28 00:43:23 -05001952 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001953 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954}
1955EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1958{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001959#ifdef CONFIG_HOTPLUG_CPU
1960 int cpu;
1961#endif
1962
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 if (!governor)
1964 return;
1965
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001966 if (cpufreq_disabled())
1967 return;
1968
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001969#ifdef CONFIG_HOTPLUG_CPU
1970 for_each_present_cpu(cpu) {
1971 if (cpu_online(cpu))
1972 continue;
1973 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1974 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1975 }
1976#endif
1977
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001978 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001980 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 return;
1982}
1983EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1984
1985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986/*********************************************************************
1987 * POLICY INTERFACE *
1988 *********************************************************************/
1989
1990/**
1991 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05001992 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1993 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 *
1995 * Reads the current cpufreq policy.
1996 */
1997int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1998{
1999 struct cpufreq_policy *cpu_policy;
2000 if (!policy)
2001 return -EINVAL;
2002
2003 cpu_policy = cpufreq_cpu_get(cpu);
2004 if (!cpu_policy)
2005 return -EINVAL;
2006
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302007 memcpy(policy, cpu_policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
2009 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 return 0;
2011}
2012EXPORT_SYMBOL(cpufreq_get_policy);
2013
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002014/*
Viresh Kumar037ce832013-10-02 14:13:16 +05302015 * policy : current policy.
2016 * new_policy: policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002017 */
Viresh Kumar037ce832013-10-02 14:13:16 +05302018static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302019 struct cpufreq_policy *new_policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002021 int ret = 0, failed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302023 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", new_policy->cpu,
2024 new_policy->min, new_policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302026 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302028 if (new_policy->min > policy->max || new_policy->max < policy->min) {
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02002029 ret = -EINVAL;
2030 goto error_out;
2031 }
2032
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 /* verify the cpu speed can be set within this limit */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302034 ret = cpufreq_driver->verify(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 if (ret)
2036 goto error_out;
2037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08002039 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302040 CPUFREQ_ADJUST, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08002043 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302044 CPUFREQ_INCOMPATIBLE, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Viresh Kumarbb176f72013-06-19 14:19:33 +05302046 /*
2047 * verify the cpu speed can be set within this limit, which might be
2048 * different to the first one
2049 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302050 ret = cpufreq_driver->verify(new_policy);
Alan Sterne041c682006-03-27 01:16:30 -08002051 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
2054 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08002055 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302056 CPUFREQ_NOTIFY, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302058 policy->min = new_policy->min;
2059 policy->max = new_policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002061 pr_debug("new min and max freqs are %u - %u kHz\n",
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302062 policy->min, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002064 if (cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302065 policy->policy = new_policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002066 pr_debug("setting range\n");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302067 ret = cpufreq_driver->setpolicy(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 } else {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302069 if (new_policy->governor != policy->governor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 /* save old, working values */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302071 struct cpufreq_governor *old_gov = policy->governor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002073 pr_debug("governor switch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
2075 /* end old governor */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302076 if (policy->governor) {
2077 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
viresh kumarad7722d2013-10-18 19:10:15 +05302078 up_write(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302079 __cpufreq_governor(policy,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002080 CPUFREQ_GOV_POLICY_EXIT);
viresh kumarad7722d2013-10-18 19:10:15 +05302081 down_write(&policy->rwsem);
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
2084 /* start new governor */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302085 policy->governor = new_policy->governor;
2086 if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
2087 if (!__cpufreq_governor(policy, CPUFREQ_GOV_START)) {
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002088 failed = 0;
Viresh Kumar955ef482013-05-16 05:09:58 +00002089 } else {
viresh kumarad7722d2013-10-18 19:10:15 +05302090 up_write(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302091 __cpufreq_governor(policy,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002092 CPUFREQ_GOV_POLICY_EXIT);
viresh kumarad7722d2013-10-18 19:10:15 +05302093 down_write(&policy->rwsem);
Viresh Kumar955ef482013-05-16 05:09:58 +00002094 }
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002095 }
2096
2097 if (failed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 /* new governor failed, so re-start old one */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002099 pr_debug("starting governor %s failed\n",
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302100 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 if (old_gov) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302102 policy->governor = old_gov;
2103 __cpufreq_governor(policy,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002104 CPUFREQ_GOV_POLICY_INIT);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302105 __cpufreq_governor(policy,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05302106 CPUFREQ_GOV_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 }
2108 ret = -EINVAL;
2109 goto error_out;
2110 }
2111 /* might be a policy change, too, so fall through */
2112 }
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002113 pr_debug("governor: change or update limits\n");
Viresh Kumar3de9bde2013-08-06 22:53:13 +05302114 ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 }
2116
Dave Jones7d5e3502006-02-02 17:03:42 -05002117error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 return ret;
2119}
2120
2121/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
2123 * @cpu: CPU which shall be re-evaluated
2124 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002125 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 * at different times.
2127 */
2128int cpufreq_update_policy(unsigned int cpu)
2129{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302130 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2131 struct cpufreq_policy new_policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02002132 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302134 if (!policy) {
Julia Lawallf1829e42008-07-25 22:44:53 +02002135 ret = -ENODEV;
2136 goto no_policy;
2137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
viresh kumarad7722d2013-10-18 19:10:15 +05302139 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002141 pr_debug("updating policy for CPU %u\n", cpu);
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302142 memcpy(&new_policy, policy, sizeof(*policy));
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302143 new_policy.min = policy->user_policy.min;
2144 new_policy.max = policy->user_policy.max;
2145 new_policy.policy = policy->user_policy.policy;
2146 new_policy.governor = policy->user_policy.governor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
Viresh Kumarbb176f72013-06-19 14:19:33 +05302148 /*
2149 * BIOS might change freq behind our back
2150 * -> ask driver for current freq and notify governors about a change
2151 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002152 if (cpufreq_driver->get) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302153 new_policy.cur = cpufreq_driver->get(cpu);
2154 if (!policy->cur) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002155 pr_debug("Driver did not initialize current freq");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302156 policy->cur = new_policy.cur;
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002157 } else {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302158 if (policy->cur != new_policy.cur && has_target())
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302159 cpufreq_out_of_sync(cpu, policy->cur,
2160 new_policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002161 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01002162 }
2163
Viresh Kumar037ce832013-10-02 14:13:16 +05302164 ret = cpufreq_set_policy(policy, &new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
viresh kumarad7722d2013-10-18 19:10:15 +05302166 up_write(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002167
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302168 cpufreq_cpu_put(policy);
Julia Lawallf1829e42008-07-25 22:44:53 +02002169no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 return ret;
2171}
2172EXPORT_SYMBOL(cpufreq_update_policy);
2173
Paul Gortmaker27609842013-06-19 13:54:04 -04002174static int cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002175 unsigned long action, void *hcpu)
2176{
2177 unsigned int cpu = (unsigned long)hcpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002178 struct device *dev;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302179 bool frozen = false;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002180
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002181 dev = get_cpu_device(cpu);
2182 if (dev) {
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302183
Rafael J. Wysockid4faadd2013-12-08 01:23:58 +01002184 if (action & CPU_TASKS_FROZEN)
2185 frozen = true;
2186
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302187 switch (action & ~CPU_TASKS_FROZEN) {
Ashok Rajc32b6b82005-10-30 14:59:54 -08002188 case CPU_ONLINE:
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302189 __cpufreq_add_dev(dev, NULL, frozen);
Srivatsa S. Bhat23d328992013-07-30 04:23:56 +05302190 cpufreq_update_policy(cpu);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002191 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302192
Ashok Rajc32b6b82005-10-30 14:59:54 -08002193 case CPU_DOWN_PREPARE:
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05302194 __cpufreq_remove_dev_prepare(dev, NULL, frozen);
Srivatsa S. Bhat1aee40a2013-09-07 01:23:27 +05302195 break;
2196
2197 case CPU_POST_DEAD:
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05302198 __cpufreq_remove_dev_finish(dev, NULL, frozen);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002199 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302200
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002201 case CPU_DOWN_FAILED:
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302202 __cpufreq_add_dev(dev, NULL, frozen);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002203 break;
2204 }
2205 }
2206 return NOTIFY_OK;
2207}
2208
Neal Buckendahl9c36f742010-06-22 22:02:44 -05002209static struct notifier_block __refdata cpufreq_cpu_notifier = {
Viresh Kumarbb176f72013-06-19 14:19:33 +05302210 .notifier_call = cpufreq_cpu_callback,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002211};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
2213/*********************************************************************
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002214 * BOOST *
2215 *********************************************************************/
2216static int cpufreq_boost_set_sw(int state)
2217{
2218 struct cpufreq_frequency_table *freq_table;
2219 struct cpufreq_policy *policy;
2220 int ret = -EINVAL;
2221
2222 list_for_each_entry(policy, &cpufreq_policy_list, policy_list) {
2223 freq_table = cpufreq_frequency_get_table(policy->cpu);
2224 if (freq_table) {
2225 ret = cpufreq_frequency_table_cpuinfo(policy,
2226 freq_table);
2227 if (ret) {
2228 pr_err("%s: Policy frequency update failed\n",
2229 __func__);
2230 break;
2231 }
2232 policy->user_policy.max = policy->max;
2233 __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
2234 }
2235 }
2236
2237 return ret;
2238}
2239
2240int cpufreq_boost_trigger_state(int state)
2241{
2242 unsigned long flags;
2243 int ret = 0;
2244
2245 if (cpufreq_driver->boost_enabled == state)
2246 return 0;
2247
2248 write_lock_irqsave(&cpufreq_driver_lock, flags);
2249 cpufreq_driver->boost_enabled = state;
2250 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2251
2252 ret = cpufreq_driver->set_boost(state);
2253 if (ret) {
2254 write_lock_irqsave(&cpufreq_driver_lock, flags);
2255 cpufreq_driver->boost_enabled = !state;
2256 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2257
2258 pr_err("%s: Cannot %s BOOST\n", __func__,
2259 state ? "enable" : "disable");
2260 }
2261
2262 return ret;
2263}
2264
2265int cpufreq_boost_supported(void)
2266{
2267 if (likely(cpufreq_driver))
2268 return cpufreq_driver->boost_supported;
2269
2270 return 0;
2271}
2272EXPORT_SYMBOL_GPL(cpufreq_boost_supported);
2273
2274int cpufreq_boost_enabled(void)
2275{
2276 return cpufreq_driver->boost_enabled;
2277}
2278EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2279
2280/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2282 *********************************************************************/
2283
2284/**
2285 * cpufreq_register_driver - register a CPU Frequency driver
2286 * @driver_data: A struct cpufreq_driver containing the values#
2287 * submitted by the CPU Frequency driver.
2288 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302289 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05002291 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 *
2293 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002294int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295{
2296 unsigned long flags;
2297 int ret;
2298
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002299 if (cpufreq_disabled())
2300 return -ENODEV;
2301
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 if (!driver_data || !driver_data->verify || !driver_data->init ||
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302303 !(driver_data->setpolicy || driver_data->target_index ||
2304 driver_data->target))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 return -EINVAL;
2306
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002307 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
2309 if (driver_data->setpolicy)
2310 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2311
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002312 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002313 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002314 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Yinghai Lu4dea58062013-09-18 21:05:20 -07002315 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002317 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002318 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002320 if (cpufreq_boost_supported()) {
2321 /*
2322 * Check if driver provides function to enable boost -
2323 * if not, use cpufreq_boost_set_sw as default
2324 */
2325 if (!cpufreq_driver->set_boost)
2326 cpufreq_driver->set_boost = cpufreq_boost_set_sw;
2327
2328 ret = cpufreq_sysfs_create_file(&boost.attr);
2329 if (ret) {
2330 pr_err("%s: cannot register global BOOST sysfs file\n",
2331 __func__);
2332 goto err_null_driver;
2333 }
2334 }
2335
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002336 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002337 if (ret)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002338 goto err_boost_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002340 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 int i;
2342 ret = -ENODEV;
2343
2344 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07002345 for (i = 0; i < nr_cpu_ids; i++)
2346 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07002348 break;
2349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
2351 /* if all ->init() calls failed, unregister */
2352 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002353 pr_debug("no CPU initialized for driver %s\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05302354 driver_data->name);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002355 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 }
2357 }
2358
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002359 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002360 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002362 return 0;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002363err_if_unreg:
2364 subsys_interface_unregister(&cpufreq_interface);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002365err_boost_unreg:
2366 if (cpufreq_boost_supported())
2367 cpufreq_sysfs_remove_file(&boost.attr);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002368err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002369 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002370 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002371 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05002372 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373}
2374EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2375
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376/**
2377 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2378 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302379 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 * the right to do so, i.e. if you have succeeded in initialising before!
2381 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2382 * currently not initialised.
2383 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002384int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385{
2386 unsigned long flags;
2387
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002388 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002391 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002393 subsys_interface_unregister(&cpufreq_interface);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002394 if (cpufreq_boost_supported())
2395 cpufreq_sysfs_remove_file(&boost.attr);
2396
Chandra Seetharaman65edc682006-06-27 02:54:08 -07002397 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
Viresh Kumar6eed9402013-08-06 22:53:11 +05302399 down_write(&cpufreq_rwsem);
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002400 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302401
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002402 cpufreq_driver = NULL;
Viresh Kumar6eed9402013-08-06 22:53:11 +05302403
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002404 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302405 up_write(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
2407 return 0;
2408}
2409EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002410
2411static int __init cpufreq_core_init(void)
2412{
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002413 if (cpufreq_disabled())
2414 return -ENODEV;
2415
Viresh Kumar2361be22013-05-17 16:09:09 +05302416 cpufreq_global_kobject = kobject_create();
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002417 BUG_ON(!cpufreq_global_kobject);
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01002418 register_syscore_ops(&cpufreq_syscore_ops);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002419
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002420 return 0;
2421}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002422core_initcall(cpufreq_core_init);