blob: 6a015ada5285720389d1191a210d541f41b429fe [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 Kumar72a4ce32013-05-17 11:26:32 +000020#include <asm/cputime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/kernel.h>
Viresh Kumar72a4ce32013-05-17 11:26:32 +000022#include <linux/kernel_stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/module.h>
24#include <linux/init.h>
25#include <linux/notifier.h>
26#include <linux/cpufreq.h>
27#include <linux/delay.h>
28#include <linux/interrupt.h>
29#include <linux/spinlock.h>
Viresh Kumar72a4ce32013-05-17 11:26:32 +000030#include <linux/tick.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/device.h>
32#include <linux/slab.h>
33#include <linux/cpu.h>
34#include <linux/completion.h>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080035#include <linux/mutex.h>
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +010036#include <linux/syscore_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Thomas Renninger6f4f2722010-04-20 13:17:36 +020038#include <trace/events/power.h>
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/**
Dave Jonescd878472006-08-11 17:59:28 -040041 * The "cpufreq driver" - the arch- or hardware-dependent low
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 * level driver of CPUFreq support, and its spinlock. This lock
43 * also protects the cpufreq_cpu_data array.
44 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +020045static struct cpufreq_driver *cpufreq_driver;
Mike Travis7a6aedf2008-03-25 15:06:53 -070046static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
Viresh Kumarbb176f72013-06-19 14:19:33 +053047static DEFINE_RWLOCK(cpufreq_driver_lock);
48static DEFINE_MUTEX(cpufreq_governor_lock);
49
Thomas Renninger084f3492007-07-09 11:35:28 -070050#ifdef CONFIG_HOTPLUG_CPU
51/* This one keeps track of the previously set governor of a removed CPU */
Dmitry Monakhove77b89f2009-10-05 00:38:55 +040052static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
Thomas Renninger084f3492007-07-09 11:35:28 -070053#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080055/*
56 * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
57 * all cpufreq/hotplug/workqueue/etc related lock issues.
58 *
59 * The rules for this semaphore:
60 * - Any routine that wants to read from the policy structure will
61 * do a down_read on this semaphore.
62 * - Any routine that will write to the policy structure and/or may take away
63 * the policy altogether (eg. CPU hotplug), will hold this lock in write
64 * mode before doing so.
65 *
66 * Additional rules:
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080067 * - Governor routines that can be called in cpufreq hotplug path should not
68 * take this sem as top level hotplug notifier handler takes this.
Mathieu Desnoyers395913d2009-06-08 13:17:31 -040069 * - Lock should not be held across
70 * __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080071 */
Tejun Heof1625062009-10-29 22:34:13 +090072static DEFINE_PER_CPU(int, cpufreq_policy_cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080073static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
74
75#define lock_policy_rwsem(mode, cpu) \
Viresh Kumarfa1d8af2013-02-07 15:38:42 +053076static int lock_policy_rwsem_##mode(int cpu) \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080077{ \
Tejun Heof1625062009-10-29 22:34:13 +090078 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080079 BUG_ON(policy_cpu == -1); \
80 down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080081 \
82 return 0; \
83}
84
85lock_policy_rwsem(read, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080086lock_policy_rwsem(write, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080087
Viresh Kumarfa1d8af2013-02-07 15:38:42 +053088#define unlock_policy_rwsem(mode, cpu) \
89static void unlock_policy_rwsem_##mode(int cpu) \
90{ \
91 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
92 BUG_ON(policy_cpu == -1); \
93 up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080094}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080095
Viresh Kumarfa1d8af2013-02-07 15:38:42 +053096unlock_policy_rwsem(read, cpu);
97unlock_policy_rwsem(write, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/* internal prototypes */
Dave Jones29464f22009-01-18 01:37:11 -0500100static int __cpufreq_governor(struct cpufreq_policy *policy,
101 unsigned int event);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800102static unsigned int __cpufreq_get(unsigned int cpu);
David Howells65f27f32006-11-22 14:55:48 +0000103static void handle_update(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105/**
Dave Jones32ee8c32006-02-28 00:43:23 -0500106 * Two notifier lists: the "policy" list is involved in the
107 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * "transition" list for kernel code that needs to handle
109 * changes to devices when the CPU clock speed changes.
110 * The mutex locks both lists.
111 */
Alan Sterne041c682006-03-27 01:16:30 -0800112static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700113static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200115static bool init_cpufreq_transition_notifier_list_called;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700116static int __init init_cpufreq_transition_notifier_list(void)
117{
118 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200119 init_cpufreq_transition_notifier_list_called = true;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700120 return 0;
121}
Linus Torvaldsb3438f82006-11-20 11:47:18 -0800122pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400124static int off __read_mostly;
Viresh Kumarda584452012-10-26 00:51:32 +0200125static int cpufreq_disabled(void)
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400126{
127 return off;
128}
129void disable_cpufreq(void)
130{
131 off = 1;
132}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133static LIST_HEAD(cpufreq_governor_list);
Dave Jones29464f22009-01-18 01:37:11 -0500134static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000136bool have_governor_per_policy(void)
137{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200138 return cpufreq_driver->have_governor_per_policy;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000139}
Viresh Kumar3f869d62013-05-16 05:09:56 +0000140EXPORT_SYMBOL_GPL(have_governor_per_policy);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000141
Viresh Kumar944e9a02013-05-16 05:09:57 +0000142struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
143{
144 if (have_governor_per_policy())
145 return &policy->kobj;
146 else
147 return cpufreq_global_kobject;
148}
149EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
150
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000151static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
152{
153 u64 idle_time;
154 u64 cur_wall_time;
155 u64 busy_time;
156
157 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
158
159 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
160 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
161 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
162 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
163 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
164 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
165
166 idle_time = cur_wall_time - busy_time;
167 if (wall)
168 *wall = cputime_to_usecs(cur_wall_time);
169
170 return cputime_to_usecs(idle_time);
171}
172
173u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
174{
175 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
176
177 if (idle_time == -1ULL)
178 return get_cpu_idle_time_jiffy(cpu, wall);
179 else if (!io_busy)
180 idle_time += get_cpu_iowait_time_us(cpu, wall);
181
182 return idle_time;
183}
184EXPORT_SYMBOL_GPL(get_cpu_idle_time);
185
Stephen Boyda9144432012-07-20 18:14:38 +0000186static struct cpufreq_policy *__cpufreq_cpu_get(unsigned int cpu, bool sysfs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 struct cpufreq_policy *data;
189 unsigned long flags;
190
Mike Travis7a6aedf2008-03-25 15:06:53 -0700191 if (cpu >= nr_cpu_ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 goto err_out;
193
194 /* get the cpufreq driver */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000195 read_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200197 if (!cpufreq_driver)
198 goto err_out_unlock;
199
200 if (!try_module_get(cpufreq_driver->owner))
201 goto err_out_unlock;
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 /* get the CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -0700204 data = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 if (!data)
207 goto err_out_put_module;
208
Stephen Boyda9144432012-07-20 18:14:38 +0000209 if (!sysfs && !kobject_get(&data->kobj))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 goto err_out_put_module;
211
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000212 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return data;
214
Dave Jones7d5e3502006-02-02 17:03:42 -0500215err_out_put_module:
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200216 module_put(cpufreq_driver->owner);
Nathan Zimmer58000432013-04-04 14:53:25 +0000217err_out_unlock:
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200218 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones7d5e3502006-02-02 17:03:42 -0500219err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return NULL;
221}
Stephen Boyda9144432012-07-20 18:14:38 +0000222
223struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
224{
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000225 if (cpufreq_disabled())
226 return NULL;
227
Stephen Boyda9144432012-07-20 18:14:38 +0000228 return __cpufreq_cpu_get(cpu, false);
229}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
231
Stephen Boyda9144432012-07-20 18:14:38 +0000232static struct cpufreq_policy *cpufreq_cpu_get_sysfs(unsigned int cpu)
233{
234 return __cpufreq_cpu_get(cpu, true);
235}
236
237static void __cpufreq_cpu_put(struct cpufreq_policy *data, bool sysfs)
238{
239 if (!sysfs)
240 kobject_put(&data->kobj);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200241 module_put(cpufreq_driver->owner);
Stephen Boyda9144432012-07-20 18:14:38 +0000242}
Dave Jones7d5e3502006-02-02 17:03:42 -0500243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244void cpufreq_cpu_put(struct cpufreq_policy *data)
245{
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000246 if (cpufreq_disabled())
247 return;
248
Stephen Boyda9144432012-07-20 18:14:38 +0000249 __cpufreq_cpu_put(data, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
252
Stephen Boyda9144432012-07-20 18:14:38 +0000253static void cpufreq_cpu_put_sysfs(struct cpufreq_policy *data)
254{
255 __cpufreq_cpu_put(data, true);
256}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
260 *********************************************************************/
261
262/**
263 * adjust_jiffies - adjust the system "loops_per_jiffy"
264 *
265 * This function alters the system "loops_per_jiffy" for the clock
266 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500267 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 * per-CPU loops_per_jiffy value wherever possible.
269 */
270#ifndef CONFIG_SMP
271static unsigned long l_p_j_ref;
Viresh Kumarbb176f72013-06-19 14:19:33 +0530272static unsigned int l_p_j_ref_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Arjan van de Ven858119e2006-01-14 13:20:43 -0800274static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 if (ci->flags & CPUFREQ_CONST_LOOPS)
277 return;
278
279 if (!l_p_j_ref_freq) {
280 l_p_j_ref = loops_per_jiffy;
281 l_p_j_ref_freq = ci->old;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200282 pr_debug("saving %lu as reference value for loops_per_jiffy; "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530283 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
Viresh Kumarbb176f72013-06-19 14:19:33 +0530285 if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700286 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530287 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
288 ci->new);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200289 pr_debug("scaling loops_per_jiffy to %lu "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530290 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292}
293#else
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530294static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
295{
296 return;
297}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298#endif
299
Viresh Kumar0956df9c2013-06-19 14:19:34 +0530300static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530301 struct cpufreq_freqs *freqs, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 BUG_ON(irqs_disabled());
304
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000305 if (cpufreq_disabled())
306 return;
307
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200308 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200309 pr_debug("notification %u of frequency transition to %u kHz\n",
Dave Jonese4472cb2006-01-31 15:53:55 -0800310 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 case CPUFREQ_PRECHANGE:
Viresh Kumar7c30ed52013-06-19 10:16:55 +0530315 if (WARN(policy->transition_ongoing,
316 "In middle of another frequency transition\n"))
317 return;
318
319 policy->transition_ongoing = true;
320
Dave Jones32ee8c32006-02-28 00:43:23 -0500321 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800322 * which is not equal to what the cpufreq core thinks is
323 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200325 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800326 if ((policy) && (policy->cpu == freqs->cpu) &&
327 (policy->cur) && (policy->cur != freqs->old)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200328 pr_debug("Warning: CPU frequency is"
Dave Jonese4472cb2006-01-31 15:53:55 -0800329 " %u, cpufreq assumed %u kHz.\n",
330 freqs->old, policy->cur);
331 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
333 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700334 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800335 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
337 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 case CPUFREQ_POSTCHANGE:
Viresh Kumar7c30ed52013-06-19 10:16:55 +0530340 if (WARN(!policy->transition_ongoing,
341 "No frequency transition in progress\n"))
342 return;
343
344 policy->transition_ongoing = false;
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200347 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
Thomas Renninger6f4f2722010-04-20 13:17:36 +0200348 (unsigned long)freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100349 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700350 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800351 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800352 if (likely(policy) && likely(policy->cpu == freqs->cpu))
353 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 break;
355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
Viresh Kumarbb176f72013-06-19 14:19:33 +0530357
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530358/**
359 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
360 * on frequency transition.
361 *
362 * This function calls the transition notifiers and the "adjust_jiffies"
363 * function. It is called twice on all CPU frequency changes that have
364 * external effects.
365 */
366void cpufreq_notify_transition(struct cpufreq_policy *policy,
367 struct cpufreq_freqs *freqs, unsigned int state)
368{
369 for_each_cpu(freqs->cpu, policy->cpus)
370 __cpufreq_notify_transition(policy, freqs, state);
371}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
373
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375/*********************************************************************
376 * SYSFS INTERFACE *
377 *********************************************************************/
378
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700379static struct cpufreq_governor *__find_governor(const char *str_governor)
380{
381 struct cpufreq_governor *t;
382
383 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
Dave Jones29464f22009-01-18 01:37:11 -0500384 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700385 return t;
386
387 return NULL;
388}
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390/**
391 * cpufreq_parse_governor - parse a governor string
392 */
Dave Jones905d77c2008-03-05 14:28:32 -0500393static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 struct cpufreq_governor **governor)
395{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700396 int err = -EINVAL;
397
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200398 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700399 goto out;
400
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200401 if (cpufreq_driver->setpolicy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
403 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700404 err = 0;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530405 } else if (!strnicmp(str_governor, "powersave",
406 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700408 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200410 } else if (cpufreq_driver->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700412
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800413 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700414
415 t = __find_governor(str_governor);
416
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700417 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700418 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700419
Kees Cook1a8e1462011-05-04 08:38:56 -0700420 mutex_unlock(&cpufreq_governor_mutex);
421 ret = request_module("cpufreq_%s", str_governor);
422 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700423
Kees Cook1a8e1462011-05-04 08:38:56 -0700424 if (ret == 0)
425 t = __find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700426 }
427
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700428 if (t != NULL) {
429 *governor = t;
430 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700432
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800433 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
Dave Jones29464f22009-01-18 01:37:11 -0500435out:
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700436 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530440 * cpufreq_per_cpu_attr_read() / show_##file_name() -
441 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 *
443 * Write out information from cpufreq_driver->policy[cpu]; object must be
444 * "unsigned int".
445 */
446
Dave Jones32ee8c32006-02-28 00:43:23 -0500447#define show_one(file_name, object) \
448static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500449(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500450{ \
Dave Jones29464f22009-01-18 01:37:11 -0500451 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
454show_one(cpuinfo_min_freq, cpuinfo.min_freq);
455show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100456show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457show_one(scaling_min_freq, min);
458show_one(scaling_max_freq, max);
459show_one(scaling_cur_freq, cur);
460
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530461static int __cpufreq_set_policy(struct cpufreq_policy *data,
462 struct cpufreq_policy *policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464/**
465 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
466 */
467#define store_one(file_name, object) \
468static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500469(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{ \
Jingoo Hanf55c9c22012-10-31 05:49:13 +0000471 unsigned int ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 struct cpufreq_policy new_policy; \
473 \
474 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
475 if (ret) \
476 return -EINVAL; \
477 \
Dave Jones29464f22009-01-18 01:37:11 -0500478 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (ret != 1) \
480 return -EINVAL; \
481 \
Thomas Renninger7970e082006-04-13 15:14:04 +0200482 ret = __cpufreq_set_policy(policy, &new_policy); \
483 policy->user_policy.object = policy->object; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 \
485 return ret ? ret : count; \
486}
487
Dave Jones29464f22009-01-18 01:37:11 -0500488store_one(scaling_min_freq, min);
489store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491/**
492 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
493 */
Dave Jones905d77c2008-03-05 14:28:32 -0500494static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
495 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800497 unsigned int cur_freq = __cpufreq_get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (!cur_freq)
499 return sprintf(buf, "<unknown>");
500 return sprintf(buf, "%u\n", cur_freq);
501}
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503/**
504 * show_scaling_governor - show the current policy for the specified CPU
505 */
Dave Jones905d77c2008-03-05 14:28:32 -0500506static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Dave Jones29464f22009-01-18 01:37:11 -0500508 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return sprintf(buf, "powersave\n");
510 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
511 return sprintf(buf, "performance\n");
512 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200513 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500514 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return -EINVAL;
516}
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518/**
519 * store_scaling_governor - store policy for the specified CPU
520 */
Dave Jones905d77c2008-03-05 14:28:32 -0500521static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
522 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Jingoo Hanf55c9c22012-10-31 05:49:13 +0000524 unsigned int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 char str_governor[16];
526 struct cpufreq_policy new_policy;
527
528 ret = cpufreq_get_policy(&new_policy, policy->cpu);
529 if (ret)
530 return ret;
531
Dave Jones29464f22009-01-18 01:37:11 -0500532 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (ret != 1)
534 return -EINVAL;
535
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530536 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
537 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return -EINVAL;
539
Viresh Kumarbb176f72013-06-19 14:19:33 +0530540 /*
541 * Do not use cpufreq_set_policy here or the user_policy.max
542 * will be wrongly overridden
543 */
Thomas Renninger7970e082006-04-13 15:14:04 +0200544 ret = __cpufreq_set_policy(policy, &new_policy);
545
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
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200572 if (!cpufreq_driver->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 kumar8bf1ac72012-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);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500700 ssize_t ret = -EINVAL;
Stephen Boyda9144432012-07-20 18:14:38 +0000701 policy = cpufreq_cpu_get_sysfs(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (!policy)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500703 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800704
705 if (lock_policy_rwsem_read(policy->cpu) < 0)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500706 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800707
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530708 if (fattr->show)
709 ret = fattr->show(policy, buf);
710 else
711 ret = -EIO;
712
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800713 unlock_policy_rwsem_read(policy->cpu);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500714fail:
Stephen Boyda9144432012-07-20 18:14:38 +0000715 cpufreq_cpu_put_sysfs(policy);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500716no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return ret;
718}
719
Dave Jones905d77c2008-03-05 14:28:32 -0500720static ssize_t store(struct kobject *kobj, struct attribute *attr,
721 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Dave Jones905d77c2008-03-05 14:28:32 -0500723 struct cpufreq_policy *policy = to_policy(kobj);
724 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500725 ssize_t ret = -EINVAL;
Stephen Boyda9144432012-07-20 18:14:38 +0000726 policy = cpufreq_cpu_get_sysfs(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (!policy)
Dave Jonesa07530b2008-03-05 14:22:25 -0500728 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800729
730 if (lock_policy_rwsem_write(policy->cpu) < 0)
Dave Jonesa07530b2008-03-05 14:22:25 -0500731 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800732
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530733 if (fattr->store)
734 ret = fattr->store(policy, buf, count);
735 else
736 ret = -EIO;
737
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800738 unlock_policy_rwsem_write(policy->cpu);
Dave Jonesa07530b2008-03-05 14:22:25 -0500739fail:
Stephen Boyda9144432012-07-20 18:14:38 +0000740 cpufreq_cpu_put_sysfs(policy);
Dave Jonesa07530b2008-03-05 14:22:25 -0500741no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 return ret;
743}
744
Dave Jones905d77c2008-03-05 14:28:32 -0500745static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Dave Jones905d77c2008-03-05 14:28:32 -0500747 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200748 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 complete(&policy->kobj_unregister);
750}
751
Emese Revfy52cf25d2010-01-19 02:58:23 +0100752static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 .show = show,
754 .store = store,
755};
756
757static struct kobj_type ktype_cpufreq = {
758 .sysfs_ops = &sysfs_ops,
759 .default_attrs = default_attrs,
760 .release = cpufreq_sysfs_release,
761};
762
Viresh Kumar2361be22013-05-17 16:09:09 +0530763struct kobject *cpufreq_global_kobject;
764EXPORT_SYMBOL(cpufreq_global_kobject);
765
766static int cpufreq_global_kobject_usage;
767
768int cpufreq_get_global_kobject(void)
769{
770 if (!cpufreq_global_kobject_usage++)
771 return kobject_add(cpufreq_global_kobject,
772 &cpu_subsys.dev_root->kobj, "%s", "cpufreq");
773
774 return 0;
775}
776EXPORT_SYMBOL(cpufreq_get_global_kobject);
777
778void cpufreq_put_global_kobject(void)
779{
780 if (!--cpufreq_global_kobject_usage)
781 kobject_del(cpufreq_global_kobject);
782}
783EXPORT_SYMBOL(cpufreq_put_global_kobject);
784
785int cpufreq_sysfs_create_file(const struct attribute *attr)
786{
787 int ret = cpufreq_get_global_kobject();
788
789 if (!ret) {
790 ret = sysfs_create_file(cpufreq_global_kobject, attr);
791 if (ret)
792 cpufreq_put_global_kobject();
793 }
794
795 return ret;
796}
797EXPORT_SYMBOL(cpufreq_sysfs_create_file);
798
799void cpufreq_sysfs_remove_file(const struct attribute *attr)
800{
801 sysfs_remove_file(cpufreq_global_kobject, attr);
802 cpufreq_put_global_kobject();
803}
804EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
805
Dave Jones19d6f7e2009-07-08 17:35:39 -0400806/* symlink affected CPUs */
Alex Chiangcf3289d02009-11-17 20:27:08 -0700807static int cpufreq_add_dev_symlink(unsigned int cpu,
808 struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400809{
810 unsigned int j;
811 int ret = 0;
812
813 for_each_cpu(j, policy->cpus) {
814 struct cpufreq_policy *managed_policy;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800815 struct device *cpu_dev;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400816
817 if (j == cpu)
818 continue;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400819
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200820 pr_debug("CPU %u already managed, adding link\n", j);
Dave Jones19d6f7e2009-07-08 17:35:39 -0400821 managed_policy = cpufreq_cpu_get(cpu);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800822 cpu_dev = get_cpu_device(j);
823 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Dave Jones19d6f7e2009-07-08 17:35:39 -0400824 "cpufreq");
825 if (ret) {
826 cpufreq_cpu_put(managed_policy);
827 return ret;
828 }
829 }
830 return ret;
831}
832
Alex Chiangcf3289d02009-11-17 20:27:08 -0700833static int cpufreq_add_dev_interface(unsigned int cpu,
834 struct cpufreq_policy *policy,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800835 struct device *dev)
Dave Jones909a6942009-07-08 18:05:42 -0400836{
Dave Jonesecf7e462009-07-08 18:48:47 -0400837 struct cpufreq_policy new_policy;
Dave Jones909a6942009-07-08 18:05:42 -0400838 struct freq_attr **drv_attr;
839 unsigned long flags;
840 int ret = 0;
841 unsigned int j;
842
843 /* prepare interface data */
844 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800845 &dev->kobj, "cpufreq");
Dave Jones909a6942009-07-08 18:05:42 -0400846 if (ret)
847 return ret;
848
849 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200850 drv_attr = cpufreq_driver->attr;
Dave Jones909a6942009-07-08 18:05:42 -0400851 while ((drv_attr) && (*drv_attr)) {
852 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
853 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200854 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400855 drv_attr++;
856 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200857 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -0400858 ret = sysfs_create_file(&policy->kobj, &cpuinfo_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->target) {
Dave Jones909a6942009-07-08 18:05:42 -0400863 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
864 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200865 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400866 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200867 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +0100868 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
869 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200870 goto err_out_kobj_put;
Thomas Renningere2f74f32009-11-19 12:31:01 +0100871 }
Dave Jones909a6942009-07-08 18:05:42 -0400872
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000873 write_lock_irqsave(&cpufreq_driver_lock, flags);
Dave Jones909a6942009-07-08 18:05:42 -0400874 for_each_cpu(j, policy->cpus) {
Dave Jones909a6942009-07-08 18:05:42 -0400875 per_cpu(cpufreq_cpu_data, j) = policy;
Tejun Heof1625062009-10-29 22:34:13 +0900876 per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
Dave Jones909a6942009-07-08 18:05:42 -0400877 }
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000878 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones909a6942009-07-08 18:05:42 -0400879
880 ret = cpufreq_add_dev_symlink(cpu, policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400881 if (ret)
882 goto err_out_kobj_put;
883
884 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
885 /* assure that the starting sequence is run in __cpufreq_set_policy */
886 policy->governor = NULL;
887
888 /* set default policy */
889 ret = __cpufreq_set_policy(policy, &new_policy);
890 policy->user_policy.policy = policy->policy;
891 policy->user_policy.governor = policy->governor;
892
893 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200894 pr_debug("setting policy failed\n");
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200895 if (cpufreq_driver->exit)
896 cpufreq_driver->exit(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400897 }
Dave Jones909a6942009-07-08 18:05:42 -0400898 return ret;
899
900err_out_kobj_put:
901 kobject_put(&policy->kobj);
902 wait_for_completion(&policy->kobj_unregister);
903 return ret;
904}
905
Viresh Kumarfcf80582013-01-29 14:39:08 +0000906#ifdef CONFIG_HOTPLUG_CPU
907static int cpufreq_add_policy_cpu(unsigned int cpu, unsigned int sibling,
908 struct device *dev)
909{
910 struct cpufreq_policy *policy;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200911 int ret = 0, has_target = !!cpufreq_driver->target;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000912 unsigned long flags;
913
914 policy = cpufreq_cpu_get(sibling);
915 WARN_ON(!policy);
916
Viresh Kumar820c6ca2013-04-22 00:48:03 +0200917 if (has_target)
918 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000919
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530920 lock_policy_rwsem_write(sibling);
921
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000922 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530923
Viresh Kumarfcf80582013-01-29 14:39:08 +0000924 cpumask_set_cpu(cpu, policy->cpus);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530925 per_cpu(cpufreq_policy_cpu, cpu) = policy->cpu;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000926 per_cpu(cpufreq_cpu_data, cpu) = policy;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000927 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000928
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530929 unlock_policy_rwsem_write(sibling);
930
Viresh Kumar820c6ca2013-04-22 00:48:03 +0200931 if (has_target) {
932 __cpufreq_governor(policy, CPUFREQ_GOV_START);
933 __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
934 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000935
Viresh Kumarfcf80582013-01-29 14:39:08 +0000936 ret = sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
937 if (ret) {
938 cpufreq_cpu_put(policy);
939 return ret;
940 }
941
942 return 0;
943}
944#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946/**
947 * cpufreq_add_dev - add a CPU device
948 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500949 * Adds the cpufreq interface for a CPU device.
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400950 *
951 * The Oracle says: try running cpufreq registration/unregistration concurrently
952 * with with cpu hotplugging and all hell will break loose. Tried to clean this
953 * mess up, but more thorough testing is needed. - Mathieu
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800955static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Viresh Kumarfcf80582013-01-29 14:39:08 +0000957 unsigned int j, cpu = dev->id;
Viresh Kumar65922462013-02-07 10:56:03 +0530958 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 unsigned long flags;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500961#ifdef CONFIG_HOTPLUG_CPU
Viresh Kumarfcf80582013-01-29 14:39:08 +0000962 struct cpufreq_governor *gov;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500963 int sibling;
964#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Ashok Rajc32b6b82005-10-30 14:59:54 -0800966 if (cpu_is_offline(cpu))
967 return 0;
968
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200969 pr_debug("adding CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971#ifdef CONFIG_SMP
972 /* check whether a different CPU already registered this
973 * CPU because it is in the same boat. */
974 policy = cpufreq_cpu_get(cpu);
975 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -0500976 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return 0;
978 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000979
980#ifdef CONFIG_HOTPLUG_CPU
981 /* Check if this cpu was hot-unplugged earlier and has siblings */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000982 read_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000983 for_each_online_cpu(sibling) {
984 struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530985 if (cp && cpumask_test_cpu(cpu, cp->related_cpus)) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000986 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000987 return cpufreq_add_policy_cpu(cpu, sibling, dev);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530988 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000989 }
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000990 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000991#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992#endif
993
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200994 if (!try_module_get(cpufreq_driver->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 ret = -EINVAL;
996 goto module_out;
997 }
998
Dave Jonese98df502005-10-20 15:17:43 -0700999 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
Dave Jones059019a2009-07-08 16:30:03 -04001000 if (!policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 goto nomem_out;
Dave Jones059019a2009-07-08 16:30:03 -04001002
1003 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001004 goto err_free_policy;
Dave Jones059019a2009-07-08 16:30:03 -04001005
1006 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001007 goto err_free_cpumask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 policy->cpu = cpu;
Viresh Kumar65922462013-02-07 10:56:03 +05301010 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Rusty Russell835481d2009-01-04 05:18:06 -08001011 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001013 /* Initially set CPU itself as the policy_cpu */
Tejun Heof1625062009-10-29 22:34:13 +09001014 per_cpu(cpufreq_policy_cpu, cpu) = cpu;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 init_completion(&policy->kobj_unregister);
David Howells65f27f32006-11-22 14:55:48 +00001017 INIT_WORK(&policy->update, handle_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 /* call driver. From then on the cpufreq must be able
1020 * to accept all calls to ->verify and ->setpolicy for this CPU
1021 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001022 ret = cpufreq_driver->init(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001024 pr_debug("initialization failed\n");
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301025 goto err_set_policy_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001027
Viresh Kumarfcf80582013-01-29 14:39:08 +00001028 /* related cpus should atleast have policy->cpus */
1029 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
1030
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001031 /*
1032 * affected cpus must always be the one, which are online. We aren't
1033 * managing offline cpus here.
1034 */
1035 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1036
Mike Chan187d9f42008-12-04 12:19:17 -08001037 policy->user_policy.min = policy->min;
1038 policy->user_policy.max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Thomas Renningera1531ac2008-07-29 22:32:58 -07001040 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1041 CPUFREQ_START, policy);
1042
Viresh Kumarfcf80582013-01-29 14:39:08 +00001043#ifdef CONFIG_HOTPLUG_CPU
1044 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
1045 if (gov) {
1046 policy->governor = gov;
1047 pr_debug("Restoring governor %s for cpu %d\n",
1048 policy->governor->name, cpu);
Thomas Renninger4bfa0422009-07-24 15:25:03 +02001049 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001050#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001052 ret = cpufreq_add_dev_interface(cpu, policy, dev);
Dave Jones19d6f7e2009-07-08 17:35:39 -04001053 if (ret)
1054 goto err_out_unregister;
Dave Jones8ff69732006-03-05 03:37:23 -05001055
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001056 kobject_uevent(&policy->kobj, KOBJ_ADD);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001057 module_put(cpufreq_driver->owner);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001058 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return 0;
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062err_out_unregister:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001063 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rusty Russell835481d2009-01-04 05:18:06 -08001064 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -07001065 per_cpu(cpufreq_cpu_data, j) = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001066 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -08001068 kobject_put(&policy->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 wait_for_completion(&policy->kobj_unregister);
1070
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301071err_set_policy_cpu:
1072 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Xiaotian Fengcad70a62010-07-20 20:11:02 +08001073 free_cpumask_var(policy->related_cpus);
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001074err_free_cpumask:
1075 free_cpumask_var(policy->cpus);
1076err_free_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 kfree(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078nomem_out:
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001079 module_put(cpufreq_driver->owner);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001080module_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return ret;
1082}
1083
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001084static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
1085{
1086 int j;
1087
1088 policy->last_cpu = policy->cpu;
1089 policy->cpu = cpu;
1090
Viresh Kumar3361b7b2013-02-04 11:38:51 +00001091 for_each_cpu(j, policy->cpus)
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001092 per_cpu(cpufreq_policy_cpu, j) = cpu;
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001093
1094#ifdef CONFIG_CPU_FREQ_TABLE
1095 cpufreq_frequency_table_update_policy_cpu(policy);
1096#endif
1097 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1098 CPUFREQ_UPDATE_POLICY_CPU, policy);
1099}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101/**
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001102 * __cpufreq_remove_dev - remove a CPU device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 *
1104 * Removes the cpufreq interface for a CPU device.
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001105 * Caller should already have policy_rwsem in write mode for this CPU.
1106 * This routine frees the rwsem before returning.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 */
Viresh Kumarbb176f72013-06-19 14:19:33 +05301108static int __cpufreq_remove_dev(struct device *dev,
1109 struct subsys_interface *sif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001111 unsigned int cpu = dev->id, ret, cpus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 unsigned long flags;
1113 struct cpufreq_policy *data;
Amerigo Wang499bca92010-03-04 03:23:46 -05001114 struct kobject *kobj;
1115 struct completion *cmp;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001116 struct device *cpu_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001118 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001120 write_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 data = per_cpu(cpufreq_cpu_data, cpu);
Mike Travis7a6aedf2008-03-25 15:06:53 -07001123 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001125 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (!data) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001128 pr_debug("%s: No cpu_data found\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001132 if (cpufreq_driver->target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001134
Jacob Shin27ecddc2011-04-27 13:32:11 -05001135#ifdef CONFIG_HOTPLUG_CPU
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001136 if (!cpufreq_driver->setpolicy)
Dirk Brandewiefa69e332013-02-06 09:02:11 -08001137 strncpy(per_cpu(cpufreq_cpu_governor, cpu),
1138 data->governor->name, CPUFREQ_NAME_LEN);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001139#endif
1140
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301141 WARN_ON(lock_policy_rwsem_write(cpu));
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001142 cpus = cpumask_weight(data->cpus);
Viresh Kumare4969eb2013-04-11 08:04:53 +00001143
1144 if (cpus > 1)
1145 cpumask_clear_cpu(cpu, data->cpus);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301146 unlock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Viresh Kumar73bf0fc2013-02-05 22:21:14 +01001148 if (cpu != data->cpu) {
1149 sysfs_remove_link(&dev->kobj, "cpufreq");
1150 } else if (cpus > 1) {
Venki Pallipadiec282972007-03-26 12:03:19 -07001151 /* first sibling now owns the new sysfs dir */
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001152 cpu_dev = get_cpu_device(cpumask_first(data->cpus));
1153 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
1154 ret = kobject_move(&data->kobj, &cpu_dev->kobj);
1155 if (ret) {
1156 pr_err("%s: Failed to move kobj: %d", __func__, ret);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301157
1158 WARN_ON(lock_policy_rwsem_write(cpu));
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001159 cpumask_set_cpu(cpu, data->cpus);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301160
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001161 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301162 per_cpu(cpufreq_cpu_data, cpu) = data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001163 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301164
1165 unlock_policy_rwsem_write(cpu);
1166
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001167 ret = sysfs_create_link(&cpu_dev->kobj, &data->kobj,
1168 "cpufreq");
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001169 return -EINVAL;
1170 }
Venki Pallipadiec282972007-03-26 12:03:19 -07001171
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301172 WARN_ON(lock_policy_rwsem_write(cpu));
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001173 update_policy_cpu(data, cpu_dev->id);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301174 unlock_policy_rwsem_write(cpu);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001175 pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
1176 __func__, cpu_dev->id, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001177 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001178
Viresh Kumard96038e2013-04-30 14:32:18 +00001179 if ((cpus == 1) && (cpufreq_driver->target))
1180 __cpufreq_governor(data, CPUFREQ_GOV_POLICY_EXIT);
1181
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001182 pr_debug("%s: removing link, cpu: %d\n", __func__, cpu);
1183 cpufreq_cpu_put(data);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001184
1185 /* If cpu is last user of policy, free policy */
1186 if (cpus == 1) {
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301187 lock_policy_rwsem_read(cpu);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001188 kobj = &data->kobj;
1189 cmp = &data->kobj_unregister;
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301190 unlock_policy_rwsem_read(cpu);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001191 kobject_put(kobj);
1192
1193 /* we need to make sure that the underlying kobj is actually
1194 * not referenced anymore by anybody before we proceed with
1195 * unloading.
1196 */
1197 pr_debug("waiting for dropping of refcount\n");
1198 wait_for_completion(cmp);
1199 pr_debug("wait complete\n");
1200
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001201 if (cpufreq_driver->exit)
1202 cpufreq_driver->exit(data);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001203
1204 free_cpumask_var(data->related_cpus);
1205 free_cpumask_var(data->cpus);
1206 kfree(data);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001207 } else if (cpufreq_driver->target) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001208 __cpufreq_governor(data, CPUFREQ_GOV_START);
1209 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301212 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 return 0;
1214}
1215
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001216static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001217{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001218 unsigned int cpu = dev->id;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001219 int retval;
1220
1221 if (cpu_is_offline(cpu))
1222 return 0;
1223
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001224 retval = __cpufreq_remove_dev(dev, sif);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001225 return retval;
1226}
1227
David Howells65f27f32006-11-22 14:55:48 +00001228static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
David Howells65f27f32006-11-22 14:55:48 +00001230 struct cpufreq_policy *policy =
1231 container_of(work, struct cpufreq_policy, update);
1232 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001233 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 cpufreq_update_policy(cpu);
1235}
1236
1237/**
Viresh Kumarbb176f72013-06-19 14:19:33 +05301238 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1239 * in deep trouble.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 * @cpu: cpu number
1241 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1242 * @new_freq: CPU frequency the CPU actually runs at
1243 *
Dave Jones29464f22009-01-18 01:37:11 -05001244 * We adjust to current frequency first, and need to clean up later.
1245 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301247static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1248 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301250 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301252 unsigned long flags;
1253
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001254 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 freqs.old = old_freq;
1258 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301259
1260 read_lock_irqsave(&cpufreq_driver_lock, flags);
1261 policy = per_cpu(cpufreq_cpu_data, cpu);
1262 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1263
1264 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1265 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266}
1267
Dave Jones32ee8c32006-02-28 00:43:23 -05001268/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301269 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001270 * @cpu: CPU number
1271 *
1272 * This is the last known freq, without actually getting it from the driver.
1273 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1274 */
1275unsigned int cpufreq_quick_get(unsigned int cpu)
1276{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001277 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301278 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001279
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001280 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1281 return cpufreq_driver->get(cpu);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001282
1283 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001284 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301285 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001286 cpufreq_cpu_put(policy);
1287 }
1288
Dave Jones4d34a672008-02-07 16:33:49 -05001289 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001290}
1291EXPORT_SYMBOL(cpufreq_quick_get);
1292
Jesse Barnes3d737102011-06-28 10:59:12 -07001293/**
1294 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1295 * @cpu: CPU number
1296 *
1297 * Just return the max possible frequency for a given CPU.
1298 */
1299unsigned int cpufreq_quick_get_max(unsigned int cpu)
1300{
1301 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1302 unsigned int ret_freq = 0;
1303
1304 if (policy) {
1305 ret_freq = policy->max;
1306 cpufreq_cpu_put(policy);
1307 }
1308
1309 return ret_freq;
1310}
1311EXPORT_SYMBOL(cpufreq_quick_get_max);
1312
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001313static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001315 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301316 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001318 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001319 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001321 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301323 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001324 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301325 /* verify no discrepancy between actual and
1326 saved value exists */
1327 if (unlikely(ret_freq != policy->cur)) {
1328 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 schedule_work(&policy->update);
1330 }
1331 }
1332
Dave Jones4d34a672008-02-07 16:33:49 -05001333 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001334}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001336/**
1337 * cpufreq_get - get the current CPU frequency (in kHz)
1338 * @cpu: CPU number
1339 *
1340 * Get the CPU current (static) CPU frequency
1341 */
1342unsigned int cpufreq_get(unsigned int cpu)
1343{
1344 unsigned int ret_freq = 0;
1345 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1346
1347 if (!policy)
1348 goto out;
1349
1350 if (unlikely(lock_policy_rwsem_read(cpu)))
1351 goto out_policy;
1352
1353 ret_freq = __cpufreq_get(cpu);
1354
1355 unlock_policy_rwsem_read(cpu);
1356
1357out_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 cpufreq_cpu_put(policy);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001359out:
Dave Jones4d34a672008-02-07 16:33:49 -05001360 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361}
1362EXPORT_SYMBOL(cpufreq_get);
1363
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001364static struct subsys_interface cpufreq_interface = {
1365 .name = "cpufreq",
1366 .subsys = &cpu_subsys,
1367 .add_dev = cpufreq_add_dev,
1368 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001369};
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001372 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1373 *
1374 * This function is only executed for the boot processor. The other CPUs
1375 * have been put offline by means of CPU hotplug.
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001376 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001377static int cpufreq_bp_suspend(void)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001378{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301379 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001380
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001381 int cpu = smp_processor_id();
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001382 struct cpufreq_policy *cpu_policy;
1383
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001384 pr_debug("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001385
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001386 /* If there's no policy for the boot CPU, we have nothing to do. */
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001387 cpu_policy = cpufreq_cpu_get(cpu);
1388 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001389 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001390
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001391 if (cpufreq_driver->suspend) {
1392 ret = cpufreq_driver->suspend(cpu_policy);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001393 if (ret)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001394 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1395 "step on CPU %u\n", cpu_policy->cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001396 }
1397
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001398 cpufreq_cpu_put(cpu_policy);
Dave Jonesc9060492008-02-07 16:32:18 -05001399 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001400}
1401
1402/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001403 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 *
1405 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001406 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1407 * restored. It will verify that the current freq is in sync with
1408 * what we believe it to be. This is a bit later than when it
1409 * should be, but nonethteless it's better than calling
1410 * cpufreq_driver->get() here which might re-enable interrupts...
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001411 *
1412 * This function is only executed for the boot CPU. The other CPUs have not
1413 * been turned on yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001415static void cpufreq_bp_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301417 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001418
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001419 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 struct cpufreq_policy *cpu_policy;
1421
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001422 pr_debug("resuming cpu %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001424 /* If there's no policy for the boot CPU, we have nothing to do. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 cpu_policy = cpufreq_cpu_get(cpu);
1426 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001427 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001429 if (cpufreq_driver->resume) {
1430 ret = cpufreq_driver->resume(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 if (ret) {
1432 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1433 "step on CPU %u\n", cpu_policy->cpu);
Dave Jonesc9060492008-02-07 16:32:18 -05001434 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 }
1436 }
1437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 schedule_work(&cpu_policy->update);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001439
Dave Jonesc9060492008-02-07 16:32:18 -05001440fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442}
1443
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001444static struct syscore_ops cpufreq_syscore_ops = {
1445 .suspend = cpufreq_bp_suspend,
1446 .resume = cpufreq_bp_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447};
1448
Borislav Petkov9d950462013-01-20 10:24:28 +00001449/**
1450 * cpufreq_get_current_driver - return current driver's name
1451 *
1452 * Return the name string of the currently loaded cpufreq driver
1453 * or NULL, if none.
1454 */
1455const char *cpufreq_get_current_driver(void)
1456{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001457 if (cpufreq_driver)
1458 return cpufreq_driver->name;
1459
1460 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001461}
1462EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
1464/*********************************************************************
1465 * NOTIFIER LISTS INTERFACE *
1466 *********************************************************************/
1467
1468/**
1469 * cpufreq_register_notifier - register a driver with cpufreq
1470 * @nb: notifier function to register
1471 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1472 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001473 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 * are notified about clock rate changes (once before and once after
1475 * the transition), or a list of drivers that are notified about
1476 * changes in cpufreq policy.
1477 *
1478 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001479 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 */
1481int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1482{
1483 int ret;
1484
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001485 if (cpufreq_disabled())
1486 return -EINVAL;
1487
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001488 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 switch (list) {
1491 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001492 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001493 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 break;
1495 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001496 ret = blocking_notifier_chain_register(
1497 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 break;
1499 default:
1500 ret = -EINVAL;
1501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503 return ret;
1504}
1505EXPORT_SYMBOL(cpufreq_register_notifier);
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507/**
1508 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1509 * @nb: notifier block to be unregistered
Viresh Kumarbb176f72013-06-19 14:19:33 +05301510 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 *
1512 * Remove a driver from the CPU frequency notifier list.
1513 *
1514 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001515 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 */
1517int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1518{
1519 int ret;
1520
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001521 if (cpufreq_disabled())
1522 return -EINVAL;
1523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 switch (list) {
1525 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001526 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001527 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 break;
1529 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001530 ret = blocking_notifier_chain_unregister(
1531 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 break;
1533 default:
1534 ret = -EINVAL;
1535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537 return ret;
1538}
1539EXPORT_SYMBOL(cpufreq_unregister_notifier);
1540
1541
1542/*********************************************************************
1543 * GOVERNORS *
1544 *********************************************************************/
1545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546int __cpufreq_driver_target(struct cpufreq_policy *policy,
1547 unsigned int target_freq,
1548 unsigned int relation)
1549{
1550 int retval = -EINVAL;
Viresh Kumar72499242012-10-31 01:28:21 +01001551 unsigned int old_target_freq = target_freq;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001552
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001553 if (cpufreq_disabled())
1554 return -ENODEV;
Viresh Kumar7c30ed52013-06-19 10:16:55 +05301555 if (policy->transition_ongoing)
1556 return -EBUSY;
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001557
Viresh Kumar72499242012-10-31 01:28:21 +01001558 /* Make sure that target_freq is within supported range */
1559 if (target_freq > policy->max)
1560 target_freq = policy->max;
1561 if (target_freq < policy->min)
1562 target_freq = policy->min;
1563
1564 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1565 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001566
1567 if (target_freq == policy->cur)
1568 return 0;
1569
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001570 if (cpufreq_driver->target)
1571 retval = cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 return retval;
1574}
1575EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577int cpufreq_driver_target(struct cpufreq_policy *policy,
1578 unsigned int target_freq,
1579 unsigned int relation)
1580{
Julia Lawallf1829e42008-07-25 22:44:53 +02001581 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001583 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
Julia Lawallf1829e42008-07-25 22:44:53 +02001584 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 ret = __cpufreq_driver_target(policy, target_freq, relation);
1587
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001588 unlock_policy_rwsem_write(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Julia Lawallf1829e42008-07-25 22:44:53 +02001590fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 return ret;
1592}
1593EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1594
venkatesh.pallipadi@intel.combf0b90e2008-08-04 11:59:07 -07001595int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu)
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001596{
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001597 if (cpufreq_disabled())
Viresh Kumara262e942013-05-31 06:15:08 +00001598 return 0;
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001599
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001600 if (!cpufreq_driver->getavg)
Viresh Kumar0676f7f2012-10-24 23:39:48 +02001601 return 0;
1602
Viresh Kumara262e942013-05-31 06:15:08 +00001603 return cpufreq_driver->getavg(policy, cpu);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001604}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001605EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001606
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001607/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001608 * when "event" is CPUFREQ_GOV_LIMITS
1609 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301611static int __cpufreq_governor(struct cpufreq_policy *policy,
1612 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
Dave Jonescc993ca2005-07-28 09:43:56 -07001614 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001615
1616 /* Only must be defined when default governor is known to have latency
1617 restrictions, like e.g. conservative or ondemand.
1618 That this is the case is already ensured in Kconfig
1619 */
1620#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1621 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1622#else
1623 struct cpufreq_governor *gov = NULL;
1624#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001625
1626 if (policy->governor->max_transition_latency &&
1627 policy->cpuinfo.transition_latency >
1628 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07001629 if (!gov)
1630 return -EINVAL;
1631 else {
1632 printk(KERN_WARNING "%s governor failed, too long"
1633 " transition latency of HW, fallback"
1634 " to %s governor\n",
1635 policy->governor->name,
1636 gov->name);
1637 policy->governor = gov;
1638 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
1641 if (!try_module_get(policy->governor->owner))
1642 return -EINVAL;
1643
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001644 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301645 policy->cpu, event);
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001646
1647 mutex_lock(&cpufreq_governor_lock);
1648 if ((!policy->governor_enabled && (event == CPUFREQ_GOV_STOP)) ||
1649 (policy->governor_enabled && (event == CPUFREQ_GOV_START))) {
1650 mutex_unlock(&cpufreq_governor_lock);
1651 return -EBUSY;
1652 }
1653
1654 if (event == CPUFREQ_GOV_STOP)
1655 policy->governor_enabled = false;
1656 else if (event == CPUFREQ_GOV_START)
1657 policy->governor_enabled = true;
1658
1659 mutex_unlock(&cpufreq_governor_lock);
1660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 ret = policy->governor->governor(policy, event);
1662
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001663 if (!ret) {
1664 if (event == CPUFREQ_GOV_POLICY_INIT)
1665 policy->governor->initialized++;
1666 else if (event == CPUFREQ_GOV_POLICY_EXIT)
1667 policy->governor->initialized--;
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001668 } else {
1669 /* Restore original values */
1670 mutex_lock(&cpufreq_governor_lock);
1671 if (event == CPUFREQ_GOV_STOP)
1672 policy->governor_enabled = true;
1673 else if (event == CPUFREQ_GOV_START)
1674 policy->governor_enabled = false;
1675 mutex_unlock(&cpufreq_governor_lock);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001676 }
Viresh Kumarb3940582013-02-01 05:42:58 +00001677
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301678 /* we keep one module reference alive for
1679 each CPU governed by this CPU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 if ((event != CPUFREQ_GOV_START) || ret)
1681 module_put(policy->governor->owner);
1682 if ((event == CPUFREQ_GOV_STOP) && !ret)
1683 module_put(policy->governor->owner);
1684
1685 return ret;
1686}
1687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688int cpufreq_register_governor(struct cpufreq_governor *governor)
1689{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001690 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692 if (!governor)
1693 return -EINVAL;
1694
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001695 if (cpufreq_disabled())
1696 return -ENODEV;
1697
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001698 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001699
Viresh Kumarb3940582013-02-01 05:42:58 +00001700 governor->initialized = 0;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001701 err = -EBUSY;
1702 if (__find_governor(governor->name) == NULL) {
1703 err = 0;
1704 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Dave Jones32ee8c32006-02-28 00:43:23 -05001707 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001708 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709}
1710EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1713{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001714#ifdef CONFIG_HOTPLUG_CPU
1715 int cpu;
1716#endif
1717
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 if (!governor)
1719 return;
1720
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001721 if (cpufreq_disabled())
1722 return;
1723
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001724#ifdef CONFIG_HOTPLUG_CPU
1725 for_each_present_cpu(cpu) {
1726 if (cpu_online(cpu))
1727 continue;
1728 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1729 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1730 }
1731#endif
1732
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001733 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001735 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 return;
1737}
1738EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1739
1740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741/*********************************************************************
1742 * POLICY INTERFACE *
1743 *********************************************************************/
1744
1745/**
1746 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05001747 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1748 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 *
1750 * Reads the current cpufreq policy.
1751 */
1752int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1753{
1754 struct cpufreq_policy *cpu_policy;
1755 if (!policy)
1756 return -EINVAL;
1757
1758 cpu_policy = cpufreq_cpu_get(cpu);
1759 if (!cpu_policy)
1760 return -EINVAL;
1761
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
1764 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 return 0;
1766}
1767EXPORT_SYMBOL(cpufreq_get_policy);
1768
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001769/*
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301770 * data : current policy.
1771 * policy : policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001772 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301773static int __cpufreq_set_policy(struct cpufreq_policy *data,
1774 struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001776 int ret = 0, failed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001778 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 policy->min, policy->max);
1780
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301781 memcpy(&policy->cpuinfo, &data->cpuinfo,
1782 sizeof(struct cpufreq_cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Yi Yang53391fa2008-01-30 13:33:34 +01001784 if (policy->min > data->max || policy->max < data->min) {
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02001785 ret = -EINVAL;
1786 goto error_out;
1787 }
1788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 /* verify the cpu speed can be set within this limit */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001790 ret = cpufreq_driver->verify(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 if (ret)
1792 goto error_out;
1793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001795 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1796 CPUFREQ_ADJUST, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001799 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1800 CPUFREQ_INCOMPATIBLE, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Viresh Kumarbb176f72013-06-19 14:19:33 +05301802 /*
1803 * verify the cpu speed can be set within this limit, which might be
1804 * different to the first one
1805 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001806 ret = cpufreq_driver->verify(policy);
Alan Sterne041c682006-03-27 01:16:30 -08001807 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
1810 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001811 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1812 CPUFREQ_NOTIFY, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Dave Jones7d5e3502006-02-02 17:03:42 -05001814 data->min = policy->min;
1815 data->max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001817 pr_debug("new min and max freqs are %u - %u kHz\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301818 data->min, data->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001820 if (cpufreq_driver->setpolicy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 data->policy = policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001822 pr_debug("setting range\n");
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001823 ret = cpufreq_driver->setpolicy(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 } else {
1825 if (policy->governor != data->governor) {
1826 /* save old, working values */
1827 struct cpufreq_governor *old_gov = data->governor;
1828
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001829 pr_debug("governor switch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
1831 /* end old governor */
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001832 if (data->governor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Viresh Kumar955ef482013-05-16 05:09:58 +00001834 unlock_policy_rwsem_write(policy->cpu);
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001835 __cpufreq_governor(data,
1836 CPUFREQ_GOV_POLICY_EXIT);
Viresh Kumar955ef482013-05-16 05:09:58 +00001837 lock_policy_rwsem_write(policy->cpu);
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
1840 /* start new governor */
1841 data->governor = policy->governor;
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001842 if (!__cpufreq_governor(data, CPUFREQ_GOV_POLICY_INIT)) {
Viresh Kumar955ef482013-05-16 05:09:58 +00001843 if (!__cpufreq_governor(data, CPUFREQ_GOV_START)) {
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001844 failed = 0;
Viresh Kumar955ef482013-05-16 05:09:58 +00001845 } else {
1846 unlock_policy_rwsem_write(policy->cpu);
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001847 __cpufreq_governor(data,
1848 CPUFREQ_GOV_POLICY_EXIT);
Viresh Kumar955ef482013-05-16 05:09:58 +00001849 lock_policy_rwsem_write(policy->cpu);
1850 }
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001851 }
1852
1853 if (failed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 /* new governor failed, so re-start old one */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001855 pr_debug("starting governor %s failed\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301856 data->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 if (old_gov) {
1858 data->governor = old_gov;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301859 __cpufreq_governor(data,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001860 CPUFREQ_GOV_POLICY_INIT);
1861 __cpufreq_governor(data,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301862 CPUFREQ_GOV_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 }
1864 ret = -EINVAL;
1865 goto error_out;
1866 }
1867 /* might be a policy change, too, so fall through */
1868 }
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001869 pr_debug("governor: change or update limits\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1871 }
1872
Dave Jones7d5e3502006-02-02 17:03:42 -05001873error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 return ret;
1875}
1876
1877/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1879 * @cpu: CPU which shall be re-evaluated
1880 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001881 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 * at different times.
1883 */
1884int cpufreq_update_policy(unsigned int cpu)
1885{
1886 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1887 struct cpufreq_policy policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02001888 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Julia Lawallf1829e42008-07-25 22:44:53 +02001890 if (!data) {
1891 ret = -ENODEV;
1892 goto no_policy;
1893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Julia Lawallf1829e42008-07-25 22:44:53 +02001895 if (unlikely(lock_policy_rwsem_write(cpu))) {
1896 ret = -EINVAL;
1897 goto fail;
1898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001900 pr_debug("updating policy for CPU %u\n", cpu);
Dave Jones7d5e3502006-02-02 17:03:42 -05001901 memcpy(&policy, data, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 policy.min = data->user_policy.min;
1903 policy.max = data->user_policy.max;
1904 policy.policy = data->user_policy.policy;
1905 policy.governor = data->user_policy.governor;
1906
Viresh Kumarbb176f72013-06-19 14:19:33 +05301907 /*
1908 * BIOS might change freq behind our back
1909 * -> ask driver for current freq and notify governors about a change
1910 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001911 if (cpufreq_driver->get) {
1912 policy.cur = cpufreq_driver->get(cpu);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001913 if (!data->cur) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001914 pr_debug("Driver did not initialize current freq");
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001915 data->cur = policy.cur;
1916 } else {
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001917 if (data->cur != policy.cur && cpufreq_driver->target)
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301918 cpufreq_out_of_sync(cpu, data->cur,
1919 policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001920 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01001921 }
1922
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 ret = __cpufreq_set_policy(data, &policy);
1924
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001925 unlock_policy_rwsem_write(cpu);
1926
Julia Lawallf1829e42008-07-25 22:44:53 +02001927fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 cpufreq_cpu_put(data);
Julia Lawallf1829e42008-07-25 22:44:53 +02001929no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 return ret;
1931}
1932EXPORT_SYMBOL(cpufreq_update_policy);
1933
Satyam Sharmadd184a02007-10-02 13:28:14 -07001934static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08001935 unsigned long action, void *hcpu)
1936{
1937 unsigned int cpu = (unsigned long)hcpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001938 struct device *dev;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001939
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001940 dev = get_cpu_device(cpu);
1941 if (dev) {
Ashok Rajc32b6b82005-10-30 14:59:54 -08001942 switch (action) {
1943 case CPU_ONLINE:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001944 cpufreq_add_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001945 break;
1946 case CPU_DOWN_PREPARE:
Srivatsa S. Bhata66b2e52013-05-15 21:47:17 +02001947 case CPU_UP_CANCELED_FROZEN:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001948 __cpufreq_remove_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001949 break;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001950 case CPU_DOWN_FAILED:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001951 cpufreq_add_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001952 break;
1953 }
1954 }
1955 return NOTIFY_OK;
1956}
1957
Neal Buckendahl9c36f742010-06-22 22:02:44 -05001958static struct notifier_block __refdata cpufreq_cpu_notifier = {
Viresh Kumarbb176f72013-06-19 14:19:33 +05301959 .notifier_call = cpufreq_cpu_callback,
Ashok Rajc32b6b82005-10-30 14:59:54 -08001960};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
1962/*********************************************************************
1963 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1964 *********************************************************************/
1965
1966/**
1967 * cpufreq_register_driver - register a CPU Frequency driver
1968 * @driver_data: A struct cpufreq_driver containing the values#
1969 * submitted by the CPU Frequency driver.
1970 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05301971 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05001973 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 *
1975 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001976int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977{
1978 unsigned long flags;
1979 int ret;
1980
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001981 if (cpufreq_disabled())
1982 return -ENODEV;
1983
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 if (!driver_data || !driver_data->verify || !driver_data->init ||
1985 ((!driver_data->setpolicy) && (!driver_data->target)))
1986 return -EINVAL;
1987
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001988 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990 if (driver_data->setpolicy)
1991 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1992
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001993 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001994 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001995 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 return -EBUSY;
1997 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001998 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001999 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002001 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002002 if (ret)
2003 goto err_null_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002005 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 int i;
2007 ret = -ENODEV;
2008
2009 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07002010 for (i = 0; i < nr_cpu_ids; i++)
2011 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07002013 break;
2014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 /* if all ->init() calls failed, unregister */
2017 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002018 pr_debug("no CPU initialized for driver %s\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05302019 driver_data->name);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002020 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 }
2022 }
2023
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002024 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002025 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002027 return 0;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002028err_if_unreg:
2029 subsys_interface_unregister(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002030err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002031 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002032 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002033 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05002034 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035}
2036EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038/**
2039 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2040 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302041 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 * the right to do so, i.e. if you have succeeded in initialising before!
2043 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2044 * currently not initialised.
2045 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002046int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
2048 unsigned long flags;
2049
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002050 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002053 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002055 subsys_interface_unregister(&cpufreq_interface);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07002056 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002058 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002059 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002060 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
2062 return 0;
2063}
2064EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002065
2066static int __init cpufreq_core_init(void)
2067{
2068 int cpu;
2069
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002070 if (cpufreq_disabled())
2071 return -ENODEV;
2072
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002073 for_each_possible_cpu(cpu) {
Tejun Heof1625062009-10-29 22:34:13 +09002074 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002075 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
2076 }
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002077
Viresh Kumar2361be22013-05-17 16:09:09 +05302078 cpufreq_global_kobject = kobject_create();
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002079 BUG_ON(!cpufreq_global_kobject);
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01002080 register_syscore_ops(&cpufreq_syscore_ops);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002081
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002082 return 0;
2083}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002084core_initcall(cpufreq_core_init);