blob: 1d041aff4d48670ccd2a7508676f61a611e8446f [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 Kumar266c13d2013-07-02 16:36:28 +0530315 if (WARN(policy->transition_ongoing ==
316 cpumask_weight(policy->cpus),
Viresh Kumar7c30ed52013-06-19 10:16:55 +0530317 "In middle of another frequency transition\n"))
318 return;
319
Viresh Kumar266c13d2013-07-02 16:36:28 +0530320 policy->transition_ongoing++;
Viresh Kumar7c30ed52013-06-19 10:16:55 +0530321
Dave Jones32ee8c32006-02-28 00:43:23 -0500322 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800323 * which is not equal to what the cpufreq core thinks is
324 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200326 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800327 if ((policy) && (policy->cpu == freqs->cpu) &&
328 (policy->cur) && (policy->cur != freqs->old)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200329 pr_debug("Warning: CPU frequency is"
Dave Jonese4472cb2006-01-31 15:53:55 -0800330 " %u, cpufreq assumed %u kHz.\n",
331 freqs->old, policy->cur);
332 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
334 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700335 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800336 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
338 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 case CPUFREQ_POSTCHANGE:
Viresh Kumar7c30ed52013-06-19 10:16:55 +0530341 if (WARN(!policy->transition_ongoing,
342 "No frequency transition in progress\n"))
343 return;
344
Viresh Kumar266c13d2013-07-02 16:36:28 +0530345 policy->transition_ongoing--;
Viresh Kumar7c30ed52013-06-19 10:16:55 +0530346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200348 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
Thomas Renninger6f4f2722010-04-20 13:17:36 +0200349 (unsigned long)freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100350 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700351 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800352 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800353 if (likely(policy) && likely(policy->cpu == freqs->cpu))
354 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 break;
356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
Viresh Kumarbb176f72013-06-19 14:19:33 +0530358
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530359/**
360 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
361 * on frequency transition.
362 *
363 * This function calls the transition notifiers and the "adjust_jiffies"
364 * function. It is called twice on all CPU frequency changes that have
365 * external effects.
366 */
367void cpufreq_notify_transition(struct cpufreq_policy *policy,
368 struct cpufreq_freqs *freqs, unsigned int state)
369{
370 for_each_cpu(freqs->cpu, policy->cpus)
371 __cpufreq_notify_transition(policy, freqs, state);
372}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
374
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376/*********************************************************************
377 * SYSFS INTERFACE *
378 *********************************************************************/
379
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700380static struct cpufreq_governor *__find_governor(const char *str_governor)
381{
382 struct cpufreq_governor *t;
383
384 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
Dave Jones29464f22009-01-18 01:37:11 -0500385 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700386 return t;
387
388 return NULL;
389}
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391/**
392 * cpufreq_parse_governor - parse a governor string
393 */
Dave Jones905d77c2008-03-05 14:28:32 -0500394static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 struct cpufreq_governor **governor)
396{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700397 int err = -EINVAL;
398
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200399 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700400 goto out;
401
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200402 if (cpufreq_driver->setpolicy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
404 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700405 err = 0;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530406 } else if (!strnicmp(str_governor, "powersave",
407 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700409 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200411 } else if (cpufreq_driver->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700413
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800414 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700415
416 t = __find_governor(str_governor);
417
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700418 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700419 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700420
Kees Cook1a8e1462011-05-04 08:38:56 -0700421 mutex_unlock(&cpufreq_governor_mutex);
422 ret = request_module("cpufreq_%s", str_governor);
423 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700424
Kees Cook1a8e1462011-05-04 08:38:56 -0700425 if (ret == 0)
426 t = __find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700427 }
428
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700429 if (t != NULL) {
430 *governor = t;
431 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700433
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800434 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
Dave Jones29464f22009-01-18 01:37:11 -0500436out:
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700437 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530441 * cpufreq_per_cpu_attr_read() / show_##file_name() -
442 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 *
444 * Write out information from cpufreq_driver->policy[cpu]; object must be
445 * "unsigned int".
446 */
447
Dave Jones32ee8c32006-02-28 00:43:23 -0500448#define show_one(file_name, object) \
449static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500450(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500451{ \
Dave Jones29464f22009-01-18 01:37:11 -0500452 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
455show_one(cpuinfo_min_freq, cpuinfo.min_freq);
456show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100457show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458show_one(scaling_min_freq, min);
459show_one(scaling_max_freq, max);
460show_one(scaling_cur_freq, cur);
461
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530462static int __cpufreq_set_policy(struct cpufreq_policy *data,
463 struct cpufreq_policy *policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465/**
466 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
467 */
468#define store_one(file_name, object) \
469static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500470(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{ \
Jingoo Hanf55c9c22012-10-31 05:49:13 +0000472 unsigned int ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 struct cpufreq_policy new_policy; \
474 \
475 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
476 if (ret) \
477 return -EINVAL; \
478 \
Dave Jones29464f22009-01-18 01:37:11 -0500479 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (ret != 1) \
481 return -EINVAL; \
482 \
Thomas Renninger7970e082006-04-13 15:14:04 +0200483 ret = __cpufreq_set_policy(policy, &new_policy); \
484 policy->user_policy.object = policy->object; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 \
486 return ret ? ret : count; \
487}
488
Dave Jones29464f22009-01-18 01:37:11 -0500489store_one(scaling_min_freq, min);
490store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492/**
493 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
494 */
Dave Jones905d77c2008-03-05 14:28:32 -0500495static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
496 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800498 unsigned int cur_freq = __cpufreq_get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (!cur_freq)
500 return sprintf(buf, "<unknown>");
501 return sprintf(buf, "%u\n", cur_freq);
502}
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504/**
505 * show_scaling_governor - show the current policy for the specified CPU
506 */
Dave Jones905d77c2008-03-05 14:28:32 -0500507static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Dave Jones29464f22009-01-18 01:37:11 -0500509 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return sprintf(buf, "powersave\n");
511 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
512 return sprintf(buf, "performance\n");
513 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200514 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500515 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return -EINVAL;
517}
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519/**
520 * store_scaling_governor - store policy for the specified CPU
521 */
Dave Jones905d77c2008-03-05 14:28:32 -0500522static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
523 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
Jingoo Hanf55c9c22012-10-31 05:49:13 +0000525 unsigned int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 char str_governor[16];
527 struct cpufreq_policy new_policy;
528
529 ret = cpufreq_get_policy(&new_policy, policy->cpu);
530 if (ret)
531 return ret;
532
Dave Jones29464f22009-01-18 01:37:11 -0500533 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (ret != 1)
535 return -EINVAL;
536
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530537 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
538 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return -EINVAL;
540
Viresh Kumarbb176f72013-06-19 14:19:33 +0530541 /*
542 * Do not use cpufreq_set_policy here or the user_policy.max
543 * will be wrongly overridden
544 */
Thomas Renninger7970e082006-04-13 15:14:04 +0200545 ret = __cpufreq_set_policy(policy, &new_policy);
546
547 policy->user_policy.policy = policy->policy;
548 policy->user_policy.governor = policy->governor;
Thomas Renninger7970e082006-04-13 15:14:04 +0200549
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530550 if (ret)
551 return ret;
552 else
553 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
556/**
557 * show_scaling_driver - show the cpufreq driver currently loaded
558 */
Dave Jones905d77c2008-03-05 14:28:32 -0500559static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200561 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
564/**
565 * show_scaling_available_governors - show the available CPUfreq governors
566 */
Dave Jones905d77c2008-03-05 14:28:32 -0500567static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
568 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 ssize_t i = 0;
571 struct cpufreq_governor *t;
572
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200573 if (!cpufreq_driver->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 i += sprintf(buf, "performance powersave");
575 goto out;
576 }
577
578 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
Dave Jones29464f22009-01-18 01:37:11 -0500579 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
580 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200582 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500584out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 i += sprintf(&buf[i], "\n");
586 return i;
587}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700588
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800589ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
591 ssize_t i = 0;
592 unsigned int cpu;
593
Rusty Russell835481d2009-01-04 05:18:06 -0800594 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (i)
596 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
597 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
598 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500599 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
601 i += sprintf(&buf[i], "\n");
602 return i;
603}
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800604EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700606/**
607 * show_related_cpus - show the CPUs affected by each transition even if
608 * hw coordination is in use
609 */
610static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
611{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800612 return cpufreq_show_cpus(policy->related_cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700613}
614
615/**
616 * show_affected_cpus - show the CPUs affected by each transition
617 */
618static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
619{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800620 return cpufreq_show_cpus(policy->cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700621}
622
Venki Pallipadi9e769882007-10-26 10:18:21 -0700623static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500624 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700625{
626 unsigned int freq = 0;
627 unsigned int ret;
628
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700629 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700630 return -EINVAL;
631
632 ret = sscanf(buf, "%u", &freq);
633 if (ret != 1)
634 return -EINVAL;
635
636 policy->governor->store_setspeed(policy, freq);
637
638 return count;
639}
640
641static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
642{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700643 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700644 return sprintf(buf, "<unsupported>\n");
645
646 return policy->governor->show_setspeed(policy, buf);
647}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Thomas Renningere2f74f32009-11-19 12:31:01 +0100649/**
viresh kumar8bf1ac722012-10-23 01:23:33 +0200650 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100651 */
652static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
653{
654 unsigned int limit;
655 int ret;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200656 if (cpufreq_driver->bios_limit) {
657 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
Thomas Renningere2f74f32009-11-19 12:31:01 +0100658 if (!ret)
659 return sprintf(buf, "%u\n", limit);
660 }
661 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
662}
663
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200664cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
665cpufreq_freq_attr_ro(cpuinfo_min_freq);
666cpufreq_freq_attr_ro(cpuinfo_max_freq);
667cpufreq_freq_attr_ro(cpuinfo_transition_latency);
668cpufreq_freq_attr_ro(scaling_available_governors);
669cpufreq_freq_attr_ro(scaling_driver);
670cpufreq_freq_attr_ro(scaling_cur_freq);
671cpufreq_freq_attr_ro(bios_limit);
672cpufreq_freq_attr_ro(related_cpus);
673cpufreq_freq_attr_ro(affected_cpus);
674cpufreq_freq_attr_rw(scaling_min_freq);
675cpufreq_freq_attr_rw(scaling_max_freq);
676cpufreq_freq_attr_rw(scaling_governor);
677cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Dave Jones905d77c2008-03-05 14:28:32 -0500679static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 &cpuinfo_min_freq.attr,
681 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100682 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 &scaling_min_freq.attr,
684 &scaling_max_freq.attr,
685 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700686 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 &scaling_governor.attr,
688 &scaling_driver.attr,
689 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700690 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 NULL
692};
693
Dave Jones29464f22009-01-18 01:37:11 -0500694#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
695#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Dave Jones29464f22009-01-18 01:37:11 -0500697static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Dave Jones905d77c2008-03-05 14:28:32 -0500699 struct cpufreq_policy *policy = to_policy(kobj);
700 struct freq_attr *fattr = to_attr(attr);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500701 ssize_t ret = -EINVAL;
Stephen Boyda9144432012-07-20 18:14:38 +0000702 policy = cpufreq_cpu_get_sysfs(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (!policy)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500704 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800705
706 if (lock_policy_rwsem_read(policy->cpu) < 0)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500707 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800708
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530709 if (fattr->show)
710 ret = fattr->show(policy, buf);
711 else
712 ret = -EIO;
713
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800714 unlock_policy_rwsem_read(policy->cpu);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500715fail:
Stephen Boyda9144432012-07-20 18:14:38 +0000716 cpufreq_cpu_put_sysfs(policy);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500717no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return ret;
719}
720
Dave Jones905d77c2008-03-05 14:28:32 -0500721static ssize_t store(struct kobject *kobj, struct attribute *attr,
722 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Dave Jones905d77c2008-03-05 14:28:32 -0500724 struct cpufreq_policy *policy = to_policy(kobj);
725 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500726 ssize_t ret = -EINVAL;
Stephen Boyda9144432012-07-20 18:14:38 +0000727 policy = cpufreq_cpu_get_sysfs(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (!policy)
Dave Jonesa07530b2008-03-05 14:22:25 -0500729 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800730
731 if (lock_policy_rwsem_write(policy->cpu) < 0)
Dave Jonesa07530b2008-03-05 14:22:25 -0500732 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800733
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530734 if (fattr->store)
735 ret = fattr->store(policy, buf, count);
736 else
737 ret = -EIO;
738
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800739 unlock_policy_rwsem_write(policy->cpu);
Dave Jonesa07530b2008-03-05 14:22:25 -0500740fail:
Stephen Boyda9144432012-07-20 18:14:38 +0000741 cpufreq_cpu_put_sysfs(policy);
Dave Jonesa07530b2008-03-05 14:22:25 -0500742no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return ret;
744}
745
Dave Jones905d77c2008-03-05 14:28:32 -0500746static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Dave Jones905d77c2008-03-05 14:28:32 -0500748 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200749 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 complete(&policy->kobj_unregister);
751}
752
Emese Revfy52cf25d2010-01-19 02:58:23 +0100753static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 .show = show,
755 .store = store,
756};
757
758static struct kobj_type ktype_cpufreq = {
759 .sysfs_ops = &sysfs_ops,
760 .default_attrs = default_attrs,
761 .release = cpufreq_sysfs_release,
762};
763
Viresh Kumar2361be22013-05-17 16:09:09 +0530764struct kobject *cpufreq_global_kobject;
765EXPORT_SYMBOL(cpufreq_global_kobject);
766
767static int cpufreq_global_kobject_usage;
768
769int cpufreq_get_global_kobject(void)
770{
771 if (!cpufreq_global_kobject_usage++)
772 return kobject_add(cpufreq_global_kobject,
773 &cpu_subsys.dev_root->kobj, "%s", "cpufreq");
774
775 return 0;
776}
777EXPORT_SYMBOL(cpufreq_get_global_kobject);
778
779void cpufreq_put_global_kobject(void)
780{
781 if (!--cpufreq_global_kobject_usage)
782 kobject_del(cpufreq_global_kobject);
783}
784EXPORT_SYMBOL(cpufreq_put_global_kobject);
785
786int cpufreq_sysfs_create_file(const struct attribute *attr)
787{
788 int ret = cpufreq_get_global_kobject();
789
790 if (!ret) {
791 ret = sysfs_create_file(cpufreq_global_kobject, attr);
792 if (ret)
793 cpufreq_put_global_kobject();
794 }
795
796 return ret;
797}
798EXPORT_SYMBOL(cpufreq_sysfs_create_file);
799
800void cpufreq_sysfs_remove_file(const struct attribute *attr)
801{
802 sysfs_remove_file(cpufreq_global_kobject, attr);
803 cpufreq_put_global_kobject();
804}
805EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
806
Dave Jones19d6f7e2009-07-08 17:35:39 -0400807/* symlink affected CPUs */
Alex Chiangcf3289d02009-11-17 20:27:08 -0700808static int cpufreq_add_dev_symlink(unsigned int cpu,
809 struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400810{
811 unsigned int j;
812 int ret = 0;
813
814 for_each_cpu(j, policy->cpus) {
815 struct cpufreq_policy *managed_policy;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800816 struct device *cpu_dev;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400817
818 if (j == cpu)
819 continue;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400820
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200821 pr_debug("CPU %u already managed, adding link\n", j);
Dave Jones19d6f7e2009-07-08 17:35:39 -0400822 managed_policy = cpufreq_cpu_get(cpu);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800823 cpu_dev = get_cpu_device(j);
824 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Dave Jones19d6f7e2009-07-08 17:35:39 -0400825 "cpufreq");
826 if (ret) {
827 cpufreq_cpu_put(managed_policy);
828 return ret;
829 }
830 }
831 return ret;
832}
833
Alex Chiangcf3289d02009-11-17 20:27:08 -0700834static int cpufreq_add_dev_interface(unsigned int cpu,
835 struct cpufreq_policy *policy,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800836 struct device *dev)
Dave Jones909a6942009-07-08 18:05:42 -0400837{
838 struct freq_attr **drv_attr;
Dave Jones909a6942009-07-08 18:05:42 -0400839 int ret = 0;
Dave Jones909a6942009-07-08 18:05:42 -0400840
841 /* prepare interface data */
842 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800843 &dev->kobj, "cpufreq");
Dave Jones909a6942009-07-08 18:05:42 -0400844 if (ret)
845 return ret;
846
847 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200848 drv_attr = cpufreq_driver->attr;
Dave Jones909a6942009-07-08 18:05:42 -0400849 while ((drv_attr) && (*drv_attr)) {
850 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
851 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200852 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400853 drv_attr++;
854 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200855 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -0400856 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
857 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200858 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400859 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200860 if (cpufreq_driver->target) {
Dave Jones909a6942009-07-08 18:05:42 -0400861 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
862 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200863 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400864 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200865 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +0100866 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
867 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200868 goto err_out_kobj_put;
Thomas Renningere2f74f32009-11-19 12:31:01 +0100869 }
Dave Jones909a6942009-07-08 18:05:42 -0400870
Dave Jones909a6942009-07-08 18:05:42 -0400871 ret = cpufreq_add_dev_symlink(cpu, policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400872 if (ret)
873 goto err_out_kobj_put;
874
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530875 return ret;
876
877err_out_kobj_put:
878 kobject_put(&policy->kobj);
879 wait_for_completion(&policy->kobj_unregister);
880 return ret;
881}
882
883static void cpufreq_init_policy(struct cpufreq_policy *policy)
884{
885 struct cpufreq_policy new_policy;
886 int ret = 0;
887
Dave Jonesecf7e462009-07-08 18:48:47 -0400888 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
889 /* assure that the starting sequence is run in __cpufreq_set_policy */
890 policy->governor = NULL;
891
892 /* set default policy */
893 ret = __cpufreq_set_policy(policy, &new_policy);
894 policy->user_policy.policy = policy->policy;
895 policy->user_policy.governor = policy->governor;
896
897 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200898 pr_debug("setting policy failed\n");
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200899 if (cpufreq_driver->exit)
900 cpufreq_driver->exit(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400901 }
Dave Jones909a6942009-07-08 18:05:42 -0400902}
903
Viresh Kumarfcf80582013-01-29 14:39:08 +0000904#ifdef CONFIG_HOTPLUG_CPU
905static int cpufreq_add_policy_cpu(unsigned int cpu, unsigned int sibling,
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +0530906 struct device *dev, bool frozen)
Viresh Kumarfcf80582013-01-29 14:39:08 +0000907{
908 struct cpufreq_policy *policy;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200909 int ret = 0, has_target = !!cpufreq_driver->target;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000910 unsigned long flags;
911
912 policy = cpufreq_cpu_get(sibling);
913 WARN_ON(!policy);
914
Viresh Kumar820c6ca2013-04-22 00:48:03 +0200915 if (has_target)
916 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000917
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530918 lock_policy_rwsem_write(sibling);
919
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000920 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530921
Viresh Kumarfcf80582013-01-29 14:39:08 +0000922 cpumask_set_cpu(cpu, policy->cpus);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530923 per_cpu(cpufreq_policy_cpu, cpu) = policy->cpu;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000924 per_cpu(cpufreq_cpu_data, cpu) = policy;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000925 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000926
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530927 unlock_policy_rwsem_write(sibling);
928
Viresh Kumar820c6ca2013-04-22 00:48:03 +0200929 if (has_target) {
930 __cpufreq_governor(policy, CPUFREQ_GOV_START);
931 __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
932 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000933
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +0530934 /* Don't touch sysfs links during light-weight init */
935 if (frozen) {
936 /* Drop the extra refcount that we took above */
Viresh Kumarfcf80582013-01-29 14:39:08 +0000937 cpufreq_cpu_put(policy);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +0530938 return 0;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000939 }
940
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +0530941 ret = sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
942 if (ret)
943 cpufreq_cpu_put(policy);
944
945 return ret;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000946}
947#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +0530949static struct cpufreq_policy *cpufreq_policy_alloc(void)
950{
951 struct cpufreq_policy *policy;
952
953 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
954 if (!policy)
955 return NULL;
956
957 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
958 goto err_free_policy;
959
960 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
961 goto err_free_cpumask;
962
963 return policy;
964
965err_free_cpumask:
966 free_cpumask_var(policy->cpus);
967err_free_policy:
968 kfree(policy);
969
970 return NULL;
971}
972
973static void cpufreq_policy_free(struct cpufreq_policy *policy)
974{
975 free_cpumask_var(policy->related_cpus);
976 free_cpumask_var(policy->cpus);
977 kfree(policy);
978}
979
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +0530980static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif,
981 bool frozen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
Viresh Kumarfcf80582013-01-29 14:39:08 +0000983 unsigned int j, cpu = dev->id;
Viresh Kumar65922462013-02-07 10:56:03 +0530984 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 unsigned long flags;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500987#ifdef CONFIG_HOTPLUG_CPU
Viresh Kumarfcf80582013-01-29 14:39:08 +0000988 struct cpufreq_governor *gov;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500989 int sibling;
990#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Ashok Rajc32b6b82005-10-30 14:59:54 -0800992 if (cpu_is_offline(cpu))
993 return 0;
994
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200995 pr_debug("adding CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997#ifdef CONFIG_SMP
998 /* check whether a different CPU already registered this
999 * CPU because it is in the same boat. */
1000 policy = cpufreq_cpu_get(cpu);
1001 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -05001002 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return 0;
1004 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001005
1006#ifdef CONFIG_HOTPLUG_CPU
1007 /* Check if this cpu was hot-unplugged earlier and has siblings */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001008 read_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +00001009 for_each_online_cpu(sibling) {
1010 struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301011 if (cp && cpumask_test_cpu(cpu, cp->related_cpus)) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001012 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301013 return cpufreq_add_policy_cpu(cpu, sibling, dev,
1014 frozen);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301015 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001016 }
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001017 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +00001018#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019#endif
1020
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001021 if (!try_module_get(cpufreq_driver->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 ret = -EINVAL;
1023 goto module_out;
1024 }
1025
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301026 policy = cpufreq_policy_alloc();
Dave Jones059019a2009-07-08 16:30:03 -04001027 if (!policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 goto nomem_out;
Dave Jones059019a2009-07-08 16:30:03 -04001029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 policy->cpu = cpu;
Viresh Kumar65922462013-02-07 10:56:03 +05301031 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Rusty Russell835481d2009-01-04 05:18:06 -08001032 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001034 /* Initially set CPU itself as the policy_cpu */
Tejun Heof1625062009-10-29 22:34:13 +09001035 per_cpu(cpufreq_policy_cpu, cpu) = cpu;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 init_completion(&policy->kobj_unregister);
David Howells65f27f32006-11-22 14:55:48 +00001038 INIT_WORK(&policy->update, handle_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 /* call driver. From then on the cpufreq must be able
1041 * to accept all calls to ->verify and ->setpolicy for this CPU
1042 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001043 ret = cpufreq_driver->init(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001045 pr_debug("initialization failed\n");
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301046 goto err_set_policy_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001048
Viresh Kumarfcf80582013-01-29 14:39:08 +00001049 /* related cpus should atleast have policy->cpus */
1050 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
1051
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001052 /*
1053 * affected cpus must always be the one, which are online. We aren't
1054 * managing offline cpus here.
1055 */
1056 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1057
Mike Chan187d9f42008-12-04 12:19:17 -08001058 policy->user_policy.min = policy->min;
1059 policy->user_policy.max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Thomas Renningera1531ac2008-07-29 22:32:58 -07001061 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1062 CPUFREQ_START, policy);
1063
Viresh Kumarfcf80582013-01-29 14:39:08 +00001064#ifdef CONFIG_HOTPLUG_CPU
1065 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
1066 if (gov) {
1067 policy->governor = gov;
1068 pr_debug("Restoring governor %s for cpu %d\n",
1069 policy->governor->name, cpu);
Thomas Renninger4bfa0422009-07-24 15:25:03 +02001070 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001071#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301073 write_lock_irqsave(&cpufreq_driver_lock, flags);
1074 for_each_cpu(j, policy->cpus) {
1075 per_cpu(cpufreq_cpu_data, j) = policy;
1076 per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
1077 }
1078 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1079
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301080 if (!frozen) {
1081 ret = cpufreq_add_dev_interface(cpu, policy, dev);
1082 if (ret)
1083 goto err_out_unregister;
1084 }
Dave Jones8ff69732006-03-05 03:37:23 -05001085
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301086 cpufreq_init_policy(policy);
1087
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001088 kobject_uevent(&policy->kobj, KOBJ_ADD);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001089 module_put(cpufreq_driver->owner);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001090 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return 0;
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094err_out_unregister:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001095 write_lock_irqsave(&cpufreq_driver_lock, flags);
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301096 for_each_cpu(j, policy->cpus) {
Mike Travis7a6aedf2008-03-25 15:06:53 -07001097 per_cpu(cpufreq_cpu_data, j) = NULL;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301098 if (j != cpu)
1099 per_cpu(cpufreq_policy_cpu, j) = -1;
1100 }
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001101 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -08001103 kobject_put(&policy->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 wait_for_completion(&policy->kobj_unregister);
1105
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301106err_set_policy_cpu:
1107 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301108 cpufreq_policy_free(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109nomem_out:
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001110 module_put(cpufreq_driver->owner);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001111module_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return ret;
1113}
1114
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301115/**
1116 * cpufreq_add_dev - add a CPU device
1117 *
1118 * Adds the cpufreq interface for a CPU device.
1119 *
1120 * The Oracle says: try running cpufreq registration/unregistration concurrently
1121 * with with cpu hotplugging and all hell will break loose. Tried to clean this
1122 * mess up, but more thorough testing is needed. - Mathieu
1123 */
1124static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1125{
1126 return __cpufreq_add_dev(dev, sif, false);
1127}
1128
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001129static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
1130{
1131 int j;
1132
1133 policy->last_cpu = policy->cpu;
1134 policy->cpu = cpu;
1135
Viresh Kumar3361b7b2013-02-04 11:38:51 +00001136 for_each_cpu(j, policy->cpus)
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001137 per_cpu(cpufreq_policy_cpu, j) = cpu;
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001138
1139#ifdef CONFIG_CPU_FREQ_TABLE
1140 cpufreq_frequency_table_update_policy_cpu(policy);
1141#endif
1142 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1143 CPUFREQ_UPDATE_POLICY_CPU, policy);
1144}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301146static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy *data,
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301147 unsigned int old_cpu, bool frozen)
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301148{
1149 struct device *cpu_dev;
1150 unsigned long flags;
1151 int ret;
1152
1153 /* first sibling now owns the new sysfs dir */
1154 cpu_dev = get_cpu_device(cpumask_first(data->cpus));
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301155
1156 /* Don't touch sysfs files during light-weight tear-down */
1157 if (frozen)
1158 return cpu_dev->id;
1159
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301160 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
1161 ret = kobject_move(&data->kobj, &cpu_dev->kobj);
1162 if (ret) {
1163 pr_err("%s: Failed to move kobj: %d", __func__, ret);
1164
1165 WARN_ON(lock_policy_rwsem_write(old_cpu));
1166 cpumask_set_cpu(old_cpu, data->cpus);
1167
1168 write_lock_irqsave(&cpufreq_driver_lock, flags);
1169 per_cpu(cpufreq_cpu_data, old_cpu) = data;
1170 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1171
1172 unlock_policy_rwsem_write(old_cpu);
1173
1174 ret = sysfs_create_link(&cpu_dev->kobj, &data->kobj,
1175 "cpufreq");
1176
1177 return -EINVAL;
1178 }
1179
1180 return cpu_dev->id;
1181}
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183/**
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001184 * __cpufreq_remove_dev - remove a CPU device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 *
1186 * Removes the cpufreq interface for a CPU device.
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001187 * Caller should already have policy_rwsem in write mode for this CPU.
1188 * This routine frees the rwsem before returning.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 */
Viresh Kumarbb176f72013-06-19 14:19:33 +05301190static int __cpufreq_remove_dev(struct device *dev,
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301191 struct subsys_interface *sif, bool frozen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301193 unsigned int cpu = dev->id, cpus;
1194 int new_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 unsigned long flags;
1196 struct cpufreq_policy *data;
Amerigo Wang499bca92010-03-04 03:23:46 -05001197 struct kobject *kobj;
1198 struct completion *cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001200 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001202 write_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 data = per_cpu(cpufreq_cpu_data, cpu);
Mike Travis7a6aedf2008-03-25 15:06:53 -07001205 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001207 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 if (!data) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001210 pr_debug("%s: No cpu_data found\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001214 if (cpufreq_driver->target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001216
Jacob Shin27ecddc2011-04-27 13:32:11 -05001217#ifdef CONFIG_HOTPLUG_CPU
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001218 if (!cpufreq_driver->setpolicy)
Dirk Brandewiefa69e332013-02-06 09:02:11 -08001219 strncpy(per_cpu(cpufreq_cpu_governor, cpu),
1220 data->governor->name, CPUFREQ_NAME_LEN);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001221#endif
1222
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301223 WARN_ON(lock_policy_rwsem_write(cpu));
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001224 cpus = cpumask_weight(data->cpus);
Viresh Kumare4969eb2013-04-11 08:04:53 +00001225
1226 if (cpus > 1)
1227 cpumask_clear_cpu(cpu, data->cpus);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301228 unlock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301230 if (cpu != data->cpu && !frozen) {
Viresh Kumar73bf0fc2013-02-05 22:21:14 +01001231 sysfs_remove_link(&dev->kobj, "cpufreq");
1232 } else if (cpus > 1) {
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301233
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301234 new_cpu = cpufreq_nominate_new_policy_cpu(data, cpu, frozen);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301235 if (new_cpu >= 0) {
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301236 WARN_ON(lock_policy_rwsem_write(cpu));
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301237 update_policy_cpu(data, new_cpu);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301238 unlock_policy_rwsem_write(cpu);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301239
1240 if (!frozen) {
1241 pr_debug("%s: policy Kobject moved to cpu: %d "
1242 "from: %d\n",__func__, new_cpu, cpu);
1243 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001244 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001245 }
Venki Pallipadiec282972007-03-26 12:03:19 -07001246
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001247 /* If cpu is last user of policy, free policy */
1248 if (cpus == 1) {
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001249 if (cpufreq_driver->target)
1250 __cpufreq_governor(data, CPUFREQ_GOV_POLICY_EXIT);
1251
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301252 lock_policy_rwsem_read(cpu);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001253 kobj = &data->kobj;
1254 cmp = &data->kobj_unregister;
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301255 unlock_policy_rwsem_read(cpu);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001256 kobject_put(kobj);
1257
1258 /* we need to make sure that the underlying kobj is actually
1259 * not referenced anymore by anybody before we proceed with
1260 * unloading.
1261 */
1262 pr_debug("waiting for dropping of refcount\n");
1263 wait_for_completion(cmp);
1264 pr_debug("wait complete\n");
1265
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001266 if (cpufreq_driver->exit)
1267 cpufreq_driver->exit(data);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001268
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301269 cpufreq_policy_free(data);
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001270 } else {
1271 pr_debug("%s: removing link, cpu: %d\n", __func__, cpu);
1272 cpufreq_cpu_put(data);
1273 if (cpufreq_driver->target) {
1274 __cpufreq_governor(data, CPUFREQ_GOV_START);
1275 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1276 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301279 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 return 0;
1281}
1282
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001283static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001284{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001285 unsigned int cpu = dev->id;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001286 int retval;
Venki Pallipadiec282972007-03-26 12:03:19 -07001287
1288 if (cpu_is_offline(cpu))
1289 return 0;
1290
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301291 retval = __cpufreq_remove_dev(dev, sif, false);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001292 return retval;
1293}
1294
David Howells65f27f32006-11-22 14:55:48 +00001295static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
David Howells65f27f32006-11-22 14:55:48 +00001297 struct cpufreq_policy *policy =
1298 container_of(work, struct cpufreq_policy, update);
1299 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001300 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 cpufreq_update_policy(cpu);
1302}
1303
1304/**
Viresh Kumarbb176f72013-06-19 14:19:33 +05301305 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1306 * in deep trouble.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 * @cpu: cpu number
1308 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1309 * @new_freq: CPU frequency the CPU actually runs at
1310 *
Dave Jones29464f22009-01-18 01:37:11 -05001311 * We adjust to current frequency first, and need to clean up later.
1312 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301314static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1315 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301317 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301319 unsigned long flags;
1320
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001321 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 freqs.old = old_freq;
1325 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301326
1327 read_lock_irqsave(&cpufreq_driver_lock, flags);
1328 policy = per_cpu(cpufreq_cpu_data, cpu);
1329 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1330
1331 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1332 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333}
1334
Dave Jones32ee8c32006-02-28 00:43:23 -05001335/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301336 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001337 * @cpu: CPU number
1338 *
1339 * This is the last known freq, without actually getting it from the driver.
1340 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1341 */
1342unsigned int cpufreq_quick_get(unsigned int cpu)
1343{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001344 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301345 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001346
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001347 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1348 return cpufreq_driver->get(cpu);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001349
1350 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001351 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301352 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001353 cpufreq_cpu_put(policy);
1354 }
1355
Dave Jones4d34a672008-02-07 16:33:49 -05001356 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001357}
1358EXPORT_SYMBOL(cpufreq_quick_get);
1359
Jesse Barnes3d737102011-06-28 10:59:12 -07001360/**
1361 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1362 * @cpu: CPU number
1363 *
1364 * Just return the max possible frequency for a given CPU.
1365 */
1366unsigned int cpufreq_quick_get_max(unsigned int cpu)
1367{
1368 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1369 unsigned int ret_freq = 0;
1370
1371 if (policy) {
1372 ret_freq = policy->max;
1373 cpufreq_cpu_put(policy);
1374 }
1375
1376 return ret_freq;
1377}
1378EXPORT_SYMBOL(cpufreq_quick_get_max);
1379
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001380static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001382 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301383 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001385 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001386 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001388 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301390 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001391 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301392 /* verify no discrepancy between actual and
1393 saved value exists */
1394 if (unlikely(ret_freq != policy->cur)) {
1395 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 schedule_work(&policy->update);
1397 }
1398 }
1399
Dave Jones4d34a672008-02-07 16:33:49 -05001400 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001401}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001403/**
1404 * cpufreq_get - get the current CPU frequency (in kHz)
1405 * @cpu: CPU number
1406 *
1407 * Get the CPU current (static) CPU frequency
1408 */
1409unsigned int cpufreq_get(unsigned int cpu)
1410{
1411 unsigned int ret_freq = 0;
1412 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1413
1414 if (!policy)
1415 goto out;
1416
1417 if (unlikely(lock_policy_rwsem_read(cpu)))
1418 goto out_policy;
1419
1420 ret_freq = __cpufreq_get(cpu);
1421
1422 unlock_policy_rwsem_read(cpu);
1423
1424out_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 cpufreq_cpu_put(policy);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001426out:
Dave Jones4d34a672008-02-07 16:33:49 -05001427 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428}
1429EXPORT_SYMBOL(cpufreq_get);
1430
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001431static struct subsys_interface cpufreq_interface = {
1432 .name = "cpufreq",
1433 .subsys = &cpu_subsys,
1434 .add_dev = cpufreq_add_dev,
1435 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001436};
1437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001439 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1440 *
1441 * This function is only executed for the boot processor. The other CPUs
1442 * have been put offline by means of CPU hotplug.
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001443 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001444static int cpufreq_bp_suspend(void)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001445{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301446 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001447
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001448 int cpu = smp_processor_id();
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001449 struct cpufreq_policy *cpu_policy;
1450
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001451 pr_debug("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001452
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001453 /* If there's no policy for the boot CPU, we have nothing to do. */
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001454 cpu_policy = cpufreq_cpu_get(cpu);
1455 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001456 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001457
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001458 if (cpufreq_driver->suspend) {
1459 ret = cpufreq_driver->suspend(cpu_policy);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001460 if (ret)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001461 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1462 "step on CPU %u\n", cpu_policy->cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001463 }
1464
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001465 cpufreq_cpu_put(cpu_policy);
Dave Jonesc9060492008-02-07 16:32:18 -05001466 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001467}
1468
1469/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001470 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 *
1472 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001473 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1474 * restored. It will verify that the current freq is in sync with
1475 * what we believe it to be. This is a bit later than when it
1476 * should be, but nonethteless it's better than calling
1477 * cpufreq_driver->get() here which might re-enable interrupts...
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001478 *
1479 * This function is only executed for the boot CPU. The other CPUs have not
1480 * been turned on yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001482static void cpufreq_bp_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301484 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001485
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001486 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 struct cpufreq_policy *cpu_policy;
1488
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001489 pr_debug("resuming cpu %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001491 /* If there's no policy for the boot CPU, we have nothing to do. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 cpu_policy = cpufreq_cpu_get(cpu);
1493 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001494 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001496 if (cpufreq_driver->resume) {
1497 ret = cpufreq_driver->resume(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 if (ret) {
1499 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1500 "step on CPU %u\n", cpu_policy->cpu);
Dave Jonesc9060492008-02-07 16:32:18 -05001501 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 }
1503 }
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 schedule_work(&cpu_policy->update);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001506
Dave Jonesc9060492008-02-07 16:32:18 -05001507fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509}
1510
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001511static struct syscore_ops cpufreq_syscore_ops = {
1512 .suspend = cpufreq_bp_suspend,
1513 .resume = cpufreq_bp_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514};
1515
Borislav Petkov9d950462013-01-20 10:24:28 +00001516/**
1517 * cpufreq_get_current_driver - return current driver's name
1518 *
1519 * Return the name string of the currently loaded cpufreq driver
1520 * or NULL, if none.
1521 */
1522const char *cpufreq_get_current_driver(void)
1523{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001524 if (cpufreq_driver)
1525 return cpufreq_driver->name;
1526
1527 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001528}
1529EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531/*********************************************************************
1532 * NOTIFIER LISTS INTERFACE *
1533 *********************************************************************/
1534
1535/**
1536 * cpufreq_register_notifier - register a driver with cpufreq
1537 * @nb: notifier function to register
1538 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1539 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001540 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 * are notified about clock rate changes (once before and once after
1542 * the transition), or a list of drivers that are notified about
1543 * changes in cpufreq policy.
1544 *
1545 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001546 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 */
1548int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1549{
1550 int ret;
1551
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001552 if (cpufreq_disabled())
1553 return -EINVAL;
1554
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001555 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 switch (list) {
1558 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001559 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001560 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 break;
1562 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001563 ret = blocking_notifier_chain_register(
1564 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 break;
1566 default:
1567 ret = -EINVAL;
1568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570 return ret;
1571}
1572EXPORT_SYMBOL(cpufreq_register_notifier);
1573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574/**
1575 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1576 * @nb: notifier block to be unregistered
Viresh Kumarbb176f72013-06-19 14:19:33 +05301577 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 *
1579 * Remove a driver from the CPU frequency notifier list.
1580 *
1581 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001582 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 */
1584int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1585{
1586 int ret;
1587
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001588 if (cpufreq_disabled())
1589 return -EINVAL;
1590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 switch (list) {
1592 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001593 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001594 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 break;
1596 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001597 ret = blocking_notifier_chain_unregister(
1598 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 break;
1600 default:
1601 ret = -EINVAL;
1602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604 return ret;
1605}
1606EXPORT_SYMBOL(cpufreq_unregister_notifier);
1607
1608
1609/*********************************************************************
1610 * GOVERNORS *
1611 *********************************************************************/
1612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613int __cpufreq_driver_target(struct cpufreq_policy *policy,
1614 unsigned int target_freq,
1615 unsigned int relation)
1616{
1617 int retval = -EINVAL;
Viresh Kumar72499242012-10-31 01:28:21 +01001618 unsigned int old_target_freq = target_freq;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001619
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001620 if (cpufreq_disabled())
1621 return -ENODEV;
Viresh Kumar7c30ed52013-06-19 10:16:55 +05301622 if (policy->transition_ongoing)
1623 return -EBUSY;
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001624
Viresh Kumar72499242012-10-31 01:28:21 +01001625 /* Make sure that target_freq is within supported range */
1626 if (target_freq > policy->max)
1627 target_freq = policy->max;
1628 if (target_freq < policy->min)
1629 target_freq = policy->min;
1630
1631 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1632 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001633
1634 if (target_freq == policy->cur)
1635 return 0;
1636
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001637 if (cpufreq_driver->target)
1638 retval = cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 return retval;
1641}
1642EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644int cpufreq_driver_target(struct cpufreq_policy *policy,
1645 unsigned int target_freq,
1646 unsigned int relation)
1647{
Julia Lawallf1829e42008-07-25 22:44:53 +02001648 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001650 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
Julia Lawallf1829e42008-07-25 22:44:53 +02001651 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
1653 ret = __cpufreq_driver_target(policy, target_freq, relation);
1654
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001655 unlock_policy_rwsem_write(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Julia Lawallf1829e42008-07-25 22:44:53 +02001657fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 return ret;
1659}
1660EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1661
venkatesh.pallipadi@intel.combf0b90e2008-08-04 11:59:07 -07001662int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu)
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001663{
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001664 if (cpufreq_disabled())
Viresh Kumara262e942013-05-31 06:15:08 +00001665 return 0;
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001666
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001667 if (!cpufreq_driver->getavg)
Viresh Kumar0676f7f2012-10-24 23:39:48 +02001668 return 0;
1669
Viresh Kumara262e942013-05-31 06:15:08 +00001670 return cpufreq_driver->getavg(policy, cpu);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001671}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001672EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001673
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001674/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001675 * when "event" is CPUFREQ_GOV_LIMITS
1676 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301678static int __cpufreq_governor(struct cpufreq_policy *policy,
1679 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680{
Dave Jonescc993ca2005-07-28 09:43:56 -07001681 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001682
1683 /* Only must be defined when default governor is known to have latency
1684 restrictions, like e.g. conservative or ondemand.
1685 That this is the case is already ensured in Kconfig
1686 */
1687#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1688 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1689#else
1690 struct cpufreq_governor *gov = NULL;
1691#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001692
1693 if (policy->governor->max_transition_latency &&
1694 policy->cpuinfo.transition_latency >
1695 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07001696 if (!gov)
1697 return -EINVAL;
1698 else {
1699 printk(KERN_WARNING "%s governor failed, too long"
1700 " transition latency of HW, fallback"
1701 " to %s governor\n",
1702 policy->governor->name,
1703 gov->name);
1704 policy->governor = gov;
1705 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
1708 if (!try_module_get(policy->governor->owner))
1709 return -EINVAL;
1710
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001711 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301712 policy->cpu, event);
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001713
1714 mutex_lock(&cpufreq_governor_lock);
1715 if ((!policy->governor_enabled && (event == CPUFREQ_GOV_STOP)) ||
1716 (policy->governor_enabled && (event == CPUFREQ_GOV_START))) {
1717 mutex_unlock(&cpufreq_governor_lock);
1718 return -EBUSY;
1719 }
1720
1721 if (event == CPUFREQ_GOV_STOP)
1722 policy->governor_enabled = false;
1723 else if (event == CPUFREQ_GOV_START)
1724 policy->governor_enabled = true;
1725
1726 mutex_unlock(&cpufreq_governor_lock);
1727
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 ret = policy->governor->governor(policy, event);
1729
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001730 if (!ret) {
1731 if (event == CPUFREQ_GOV_POLICY_INIT)
1732 policy->governor->initialized++;
1733 else if (event == CPUFREQ_GOV_POLICY_EXIT)
1734 policy->governor->initialized--;
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001735 } else {
1736 /* Restore original values */
1737 mutex_lock(&cpufreq_governor_lock);
1738 if (event == CPUFREQ_GOV_STOP)
1739 policy->governor_enabled = true;
1740 else if (event == CPUFREQ_GOV_START)
1741 policy->governor_enabled = false;
1742 mutex_unlock(&cpufreq_governor_lock);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001743 }
Viresh Kumarb3940582013-02-01 05:42:58 +00001744
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301745 /* we keep one module reference alive for
1746 each CPU governed by this CPU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 if ((event != CPUFREQ_GOV_START) || ret)
1748 module_put(policy->governor->owner);
1749 if ((event == CPUFREQ_GOV_STOP) && !ret)
1750 module_put(policy->governor->owner);
1751
1752 return ret;
1753}
1754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755int cpufreq_register_governor(struct cpufreq_governor *governor)
1756{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001757 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
1759 if (!governor)
1760 return -EINVAL;
1761
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001762 if (cpufreq_disabled())
1763 return -ENODEV;
1764
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001765 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001766
Viresh Kumarb3940582013-02-01 05:42:58 +00001767 governor->initialized = 0;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001768 err = -EBUSY;
1769 if (__find_governor(governor->name) == NULL) {
1770 err = 0;
1771 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Dave Jones32ee8c32006-02-28 00:43:23 -05001774 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001775 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776}
1777EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1780{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001781#ifdef CONFIG_HOTPLUG_CPU
1782 int cpu;
1783#endif
1784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 if (!governor)
1786 return;
1787
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001788 if (cpufreq_disabled())
1789 return;
1790
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001791#ifdef CONFIG_HOTPLUG_CPU
1792 for_each_present_cpu(cpu) {
1793 if (cpu_online(cpu))
1794 continue;
1795 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1796 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1797 }
1798#endif
1799
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001800 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001802 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 return;
1804}
1805EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1806
1807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808/*********************************************************************
1809 * POLICY INTERFACE *
1810 *********************************************************************/
1811
1812/**
1813 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05001814 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1815 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 *
1817 * Reads the current cpufreq policy.
1818 */
1819int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1820{
1821 struct cpufreq_policy *cpu_policy;
1822 if (!policy)
1823 return -EINVAL;
1824
1825 cpu_policy = cpufreq_cpu_get(cpu);
1826 if (!cpu_policy)
1827 return -EINVAL;
1828
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
1831 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 return 0;
1833}
1834EXPORT_SYMBOL(cpufreq_get_policy);
1835
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001836/*
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301837 * data : current policy.
1838 * policy : policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001839 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301840static int __cpufreq_set_policy(struct cpufreq_policy *data,
1841 struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001843 int ret = 0, failed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001845 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 policy->min, policy->max);
1847
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301848 memcpy(&policy->cpuinfo, &data->cpuinfo,
1849 sizeof(struct cpufreq_cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Yi Yang53391fa2008-01-30 13:33:34 +01001851 if (policy->min > data->max || policy->max < data->min) {
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02001852 ret = -EINVAL;
1853 goto error_out;
1854 }
1855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 /* verify the cpu speed can be set within this limit */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001857 ret = cpufreq_driver->verify(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 if (ret)
1859 goto error_out;
1860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001862 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1863 CPUFREQ_ADJUST, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
1865 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001866 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1867 CPUFREQ_INCOMPATIBLE, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Viresh Kumarbb176f72013-06-19 14:19:33 +05301869 /*
1870 * verify the cpu speed can be set within this limit, which might be
1871 * different to the first one
1872 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001873 ret = cpufreq_driver->verify(policy);
Alan Sterne041c682006-03-27 01:16:30 -08001874 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001878 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1879 CPUFREQ_NOTIFY, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Dave Jones7d5e3502006-02-02 17:03:42 -05001881 data->min = policy->min;
1882 data->max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001884 pr_debug("new min and max freqs are %u - %u kHz\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301885 data->min, data->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001887 if (cpufreq_driver->setpolicy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 data->policy = policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001889 pr_debug("setting range\n");
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001890 ret = cpufreq_driver->setpolicy(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 } else {
1892 if (policy->governor != data->governor) {
1893 /* save old, working values */
1894 struct cpufreq_governor *old_gov = data->governor;
1895
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001896 pr_debug("governor switch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
1898 /* end old governor */
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001899 if (data->governor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Viresh Kumar955ef482013-05-16 05:09:58 +00001901 unlock_policy_rwsem_write(policy->cpu);
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001902 __cpufreq_governor(data,
1903 CPUFREQ_GOV_POLICY_EXIT);
Viresh Kumar955ef482013-05-16 05:09:58 +00001904 lock_policy_rwsem_write(policy->cpu);
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
1907 /* start new governor */
1908 data->governor = policy->governor;
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001909 if (!__cpufreq_governor(data, CPUFREQ_GOV_POLICY_INIT)) {
Viresh Kumar955ef482013-05-16 05:09:58 +00001910 if (!__cpufreq_governor(data, CPUFREQ_GOV_START)) {
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001911 failed = 0;
Viresh Kumar955ef482013-05-16 05:09:58 +00001912 } else {
1913 unlock_policy_rwsem_write(policy->cpu);
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001914 __cpufreq_governor(data,
1915 CPUFREQ_GOV_POLICY_EXIT);
Viresh Kumar955ef482013-05-16 05:09:58 +00001916 lock_policy_rwsem_write(policy->cpu);
1917 }
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001918 }
1919
1920 if (failed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 /* new governor failed, so re-start old one */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001922 pr_debug("starting governor %s failed\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301923 data->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 if (old_gov) {
1925 data->governor = old_gov;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301926 __cpufreq_governor(data,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001927 CPUFREQ_GOV_POLICY_INIT);
1928 __cpufreq_governor(data,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301929 CPUFREQ_GOV_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 }
1931 ret = -EINVAL;
1932 goto error_out;
1933 }
1934 /* might be a policy change, too, so fall through */
1935 }
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001936 pr_debug("governor: change or update limits\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1938 }
1939
Dave Jones7d5e3502006-02-02 17:03:42 -05001940error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 return ret;
1942}
1943
1944/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1946 * @cpu: CPU which shall be re-evaluated
1947 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001948 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 * at different times.
1950 */
1951int cpufreq_update_policy(unsigned int cpu)
1952{
1953 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1954 struct cpufreq_policy policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02001955 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Julia Lawallf1829e42008-07-25 22:44:53 +02001957 if (!data) {
1958 ret = -ENODEV;
1959 goto no_policy;
1960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Julia Lawallf1829e42008-07-25 22:44:53 +02001962 if (unlikely(lock_policy_rwsem_write(cpu))) {
1963 ret = -EINVAL;
1964 goto fail;
1965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001967 pr_debug("updating policy for CPU %u\n", cpu);
Dave Jones7d5e3502006-02-02 17:03:42 -05001968 memcpy(&policy, data, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 policy.min = data->user_policy.min;
1970 policy.max = data->user_policy.max;
1971 policy.policy = data->user_policy.policy;
1972 policy.governor = data->user_policy.governor;
1973
Viresh Kumarbb176f72013-06-19 14:19:33 +05301974 /*
1975 * BIOS might change freq behind our back
1976 * -> ask driver for current freq and notify governors about a change
1977 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001978 if (cpufreq_driver->get) {
1979 policy.cur = cpufreq_driver->get(cpu);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001980 if (!data->cur) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001981 pr_debug("Driver did not initialize current freq");
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001982 data->cur = policy.cur;
1983 } else {
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001984 if (data->cur != policy.cur && cpufreq_driver->target)
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301985 cpufreq_out_of_sync(cpu, data->cur,
1986 policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001987 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01001988 }
1989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 ret = __cpufreq_set_policy(data, &policy);
1991
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001992 unlock_policy_rwsem_write(cpu);
1993
Julia Lawallf1829e42008-07-25 22:44:53 +02001994fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 cpufreq_cpu_put(data);
Julia Lawallf1829e42008-07-25 22:44:53 +02001996no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 return ret;
1998}
1999EXPORT_SYMBOL(cpufreq_update_policy);
2000
Paul Gortmaker27609842013-06-19 13:54:04 -04002001static int cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002002 unsigned long action, void *hcpu)
2003{
2004 unsigned int cpu = (unsigned long)hcpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002005 struct device *dev;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002006
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002007 dev = get_cpu_device(cpu);
2008 if (dev) {
Ashok Rajc32b6b82005-10-30 14:59:54 -08002009 switch (action) {
2010 case CPU_ONLINE:
Srivatsa S. Bhataae760e2013-07-12 03:45:37 +05302011 case CPU_ONLINE_FROZEN:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002012 cpufreq_add_dev(dev, NULL);
Srivatsa S. Bhat23d328992013-07-30 04:23:56 +05302013 cpufreq_update_policy(cpu);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002014 break;
2015 case CPU_DOWN_PREPARE:
Srivatsa S. Bhataae760e2013-07-12 03:45:37 +05302016 case CPU_DOWN_PREPARE_FROZEN:
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05302017 __cpufreq_remove_dev(dev, NULL, false);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002018 break;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002019 case CPU_DOWN_FAILED:
Srivatsa S. Bhataae760e2013-07-12 03:45:37 +05302020 case CPU_DOWN_FAILED_FROZEN:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002021 cpufreq_add_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002022 break;
2023 }
2024 }
2025 return NOTIFY_OK;
2026}
2027
Neal Buckendahl9c36f742010-06-22 22:02:44 -05002028static struct notifier_block __refdata cpufreq_cpu_notifier = {
Viresh Kumarbb176f72013-06-19 14:19:33 +05302029 .notifier_call = cpufreq_cpu_callback,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002030};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
2032/*********************************************************************
2033 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2034 *********************************************************************/
2035
2036/**
2037 * cpufreq_register_driver - register a CPU Frequency driver
2038 * @driver_data: A struct cpufreq_driver containing the values#
2039 * submitted by the CPU Frequency driver.
2040 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302041 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05002043 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 *
2045 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002046int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
2048 unsigned long flags;
2049 int ret;
2050
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002051 if (cpufreq_disabled())
2052 return -ENODEV;
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 if (!driver_data || !driver_data->verify || !driver_data->init ||
2055 ((!driver_data->setpolicy) && (!driver_data->target)))
2056 return -EINVAL;
2057
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002058 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
2060 if (driver_data->setpolicy)
2061 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2062
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002063 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002064 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002065 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 return -EBUSY;
2067 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002068 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002069 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002071 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002072 if (ret)
2073 goto err_null_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002075 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 int i;
2077 ret = -ENODEV;
2078
2079 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07002080 for (i = 0; i < nr_cpu_ids; i++)
2081 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07002083 break;
2084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
2086 /* if all ->init() calls failed, unregister */
2087 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002088 pr_debug("no CPU initialized for driver %s\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05302089 driver_data->name);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002090 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 }
2092 }
2093
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002094 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002095 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002097 return 0;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002098err_if_unreg:
2099 subsys_interface_unregister(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002100err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002101 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002102 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002103 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05002104 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105}
2106EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2107
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108/**
2109 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2110 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302111 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 * the right to do so, i.e. if you have succeeded in initialising before!
2113 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2114 * currently not initialised.
2115 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002116int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117{
2118 unsigned long flags;
2119
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002120 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002123 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002125 subsys_interface_unregister(&cpufreq_interface);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07002126 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002128 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002129 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002130 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
2132 return 0;
2133}
2134EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002135
2136static int __init cpufreq_core_init(void)
2137{
2138 int cpu;
2139
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002140 if (cpufreq_disabled())
2141 return -ENODEV;
2142
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002143 for_each_possible_cpu(cpu) {
Tejun Heof1625062009-10-29 22:34:13 +09002144 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002145 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
2146 }
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002147
Viresh Kumar2361be22013-05-17 16:09:09 +05302148 cpufreq_global_kobject = kobject_create();
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002149 BUG_ON(!cpufreq_global_kobject);
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01002150 register_syscore_ops(&cpufreq_syscore_ops);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002151
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002152 return 0;
2153}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002154core_initcall(cpufreq_core_init);