blob: d976e222f10fb3f50143963a435638257bb314f4 [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:
Dave Jones32ee8c32006-02-28 00:43:23 -0500315 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800316 * which is not equal to what the cpufreq core thinks is
317 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200319 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800320 if ((policy) && (policy->cpu == freqs->cpu) &&
321 (policy->cur) && (policy->cur != freqs->old)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200322 pr_debug("Warning: CPU frequency is"
Dave Jonese4472cb2006-01-31 15:53:55 -0800323 " %u, cpufreq assumed %u kHz.\n",
324 freqs->old, policy->cur);
325 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
327 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700328 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800329 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
331 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 case CPUFREQ_POSTCHANGE:
334 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200335 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
Thomas Renninger6f4f2722010-04-20 13:17:36 +0200336 (unsigned long)freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100337 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700338 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800339 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800340 if (likely(policy) && likely(policy->cpu == freqs->cpu))
341 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 break;
343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
Viresh Kumarbb176f72013-06-19 14:19:33 +0530345
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530346/**
347 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
348 * on frequency transition.
349 *
350 * This function calls the transition notifiers and the "adjust_jiffies"
351 * function. It is called twice on all CPU frequency changes that have
352 * external effects.
353 */
354void cpufreq_notify_transition(struct cpufreq_policy *policy,
355 struct cpufreq_freqs *freqs, unsigned int state)
356{
357 for_each_cpu(freqs->cpu, policy->cpus)
358 __cpufreq_notify_transition(policy, freqs, state);
359}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
361
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363/*********************************************************************
364 * SYSFS INTERFACE *
365 *********************************************************************/
366
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700367static struct cpufreq_governor *__find_governor(const char *str_governor)
368{
369 struct cpufreq_governor *t;
370
371 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
Dave Jones29464f22009-01-18 01:37:11 -0500372 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700373 return t;
374
375 return NULL;
376}
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378/**
379 * cpufreq_parse_governor - parse a governor string
380 */
Dave Jones905d77c2008-03-05 14:28:32 -0500381static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 struct cpufreq_governor **governor)
383{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700384 int err = -EINVAL;
385
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200386 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700387 goto out;
388
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200389 if (cpufreq_driver->setpolicy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
391 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700392 err = 0;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530393 } else if (!strnicmp(str_governor, "powersave",
394 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700396 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200398 } else if (cpufreq_driver->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700400
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800401 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700402
403 t = __find_governor(str_governor);
404
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700405 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700406 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700407
Kees Cook1a8e1462011-05-04 08:38:56 -0700408 mutex_unlock(&cpufreq_governor_mutex);
409 ret = request_module("cpufreq_%s", str_governor);
410 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700411
Kees Cook1a8e1462011-05-04 08:38:56 -0700412 if (ret == 0)
413 t = __find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700414 }
415
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700416 if (t != NULL) {
417 *governor = t;
418 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700420
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800421 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
Dave Jones29464f22009-01-18 01:37:11 -0500423out:
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700424 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530428 * cpufreq_per_cpu_attr_read() / show_##file_name() -
429 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 *
431 * Write out information from cpufreq_driver->policy[cpu]; object must be
432 * "unsigned int".
433 */
434
Dave Jones32ee8c32006-02-28 00:43:23 -0500435#define show_one(file_name, object) \
436static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500437(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500438{ \
Dave Jones29464f22009-01-18 01:37:11 -0500439 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
442show_one(cpuinfo_min_freq, cpuinfo.min_freq);
443show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100444show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445show_one(scaling_min_freq, min);
446show_one(scaling_max_freq, max);
447show_one(scaling_cur_freq, cur);
448
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530449static int __cpufreq_set_policy(struct cpufreq_policy *data,
450 struct cpufreq_policy *policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452/**
453 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
454 */
455#define store_one(file_name, object) \
456static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500457(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{ \
Jingoo Hanf55c9c22012-10-31 05:49:13 +0000459 unsigned int ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 struct cpufreq_policy new_policy; \
461 \
462 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
463 if (ret) \
464 return -EINVAL; \
465 \
Dave Jones29464f22009-01-18 01:37:11 -0500466 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (ret != 1) \
468 return -EINVAL; \
469 \
Thomas Renninger7970e082006-04-13 15:14:04 +0200470 ret = __cpufreq_set_policy(policy, &new_policy); \
471 policy->user_policy.object = policy->object; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 \
473 return ret ? ret : count; \
474}
475
Dave Jones29464f22009-01-18 01:37:11 -0500476store_one(scaling_min_freq, min);
477store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479/**
480 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
481 */
Dave Jones905d77c2008-03-05 14:28:32 -0500482static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
483 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800485 unsigned int cur_freq = __cpufreq_get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (!cur_freq)
487 return sprintf(buf, "<unknown>");
488 return sprintf(buf, "%u\n", cur_freq);
489}
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491/**
492 * show_scaling_governor - show the current policy for the specified CPU
493 */
Dave Jones905d77c2008-03-05 14:28:32 -0500494static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Dave Jones29464f22009-01-18 01:37:11 -0500496 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return sprintf(buf, "powersave\n");
498 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
499 return sprintf(buf, "performance\n");
500 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200501 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500502 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return -EINVAL;
504}
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506/**
507 * store_scaling_governor - store policy for the specified CPU
508 */
Dave Jones905d77c2008-03-05 14:28:32 -0500509static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
510 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Jingoo Hanf55c9c22012-10-31 05:49:13 +0000512 unsigned int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 char str_governor[16];
514 struct cpufreq_policy new_policy;
515
516 ret = cpufreq_get_policy(&new_policy, policy->cpu);
517 if (ret)
518 return ret;
519
Dave Jones29464f22009-01-18 01:37:11 -0500520 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (ret != 1)
522 return -EINVAL;
523
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530524 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
525 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return -EINVAL;
527
Viresh Kumarbb176f72013-06-19 14:19:33 +0530528 /*
529 * Do not use cpufreq_set_policy here or the user_policy.max
530 * will be wrongly overridden
531 */
Thomas Renninger7970e082006-04-13 15:14:04 +0200532 ret = __cpufreq_set_policy(policy, &new_policy);
533
534 policy->user_policy.policy = policy->policy;
535 policy->user_policy.governor = policy->governor;
Thomas Renninger7970e082006-04-13 15:14:04 +0200536
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530537 if (ret)
538 return ret;
539 else
540 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
543/**
544 * show_scaling_driver - show the cpufreq driver currently loaded
545 */
Dave Jones905d77c2008-03-05 14:28:32 -0500546static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200548 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
551/**
552 * show_scaling_available_governors - show the available CPUfreq governors
553 */
Dave Jones905d77c2008-03-05 14:28:32 -0500554static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
555 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
557 ssize_t i = 0;
558 struct cpufreq_governor *t;
559
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200560 if (!cpufreq_driver->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 i += sprintf(buf, "performance powersave");
562 goto out;
563 }
564
565 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
Dave Jones29464f22009-01-18 01:37:11 -0500566 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
567 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200569 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500571out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 i += sprintf(&buf[i], "\n");
573 return i;
574}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700575
Rusty Russell835481d2009-01-04 05:18:06 -0800576static ssize_t show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
578 ssize_t i = 0;
579 unsigned int cpu;
580
Rusty Russell835481d2009-01-04 05:18:06 -0800581 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (i)
583 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
584 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
585 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500586 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
588 i += sprintf(&buf[i], "\n");
589 return i;
590}
591
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700592/**
593 * show_related_cpus - show the CPUs affected by each transition even if
594 * hw coordination is in use
595 */
596static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
597{
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700598 return show_cpus(policy->related_cpus, buf);
599}
600
601/**
602 * show_affected_cpus - show the CPUs affected by each transition
603 */
604static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
605{
606 return show_cpus(policy->cpus, buf);
607}
608
Venki Pallipadi9e769882007-10-26 10:18:21 -0700609static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500610 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700611{
612 unsigned int freq = 0;
613 unsigned int ret;
614
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700615 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700616 return -EINVAL;
617
618 ret = sscanf(buf, "%u", &freq);
619 if (ret != 1)
620 return -EINVAL;
621
622 policy->governor->store_setspeed(policy, freq);
623
624 return count;
625}
626
627static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
628{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700629 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700630 return sprintf(buf, "<unsupported>\n");
631
632 return policy->governor->show_setspeed(policy, buf);
633}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Thomas Renningere2f74f32009-11-19 12:31:01 +0100635/**
viresh kumar8bf1ac722012-10-23 01:23:33 +0200636 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100637 */
638static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
639{
640 unsigned int limit;
641 int ret;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200642 if (cpufreq_driver->bios_limit) {
643 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
Thomas Renningere2f74f32009-11-19 12:31:01 +0100644 if (!ret)
645 return sprintf(buf, "%u\n", limit);
646 }
647 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
648}
649
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200650cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
651cpufreq_freq_attr_ro(cpuinfo_min_freq);
652cpufreq_freq_attr_ro(cpuinfo_max_freq);
653cpufreq_freq_attr_ro(cpuinfo_transition_latency);
654cpufreq_freq_attr_ro(scaling_available_governors);
655cpufreq_freq_attr_ro(scaling_driver);
656cpufreq_freq_attr_ro(scaling_cur_freq);
657cpufreq_freq_attr_ro(bios_limit);
658cpufreq_freq_attr_ro(related_cpus);
659cpufreq_freq_attr_ro(affected_cpus);
660cpufreq_freq_attr_rw(scaling_min_freq);
661cpufreq_freq_attr_rw(scaling_max_freq);
662cpufreq_freq_attr_rw(scaling_governor);
663cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Dave Jones905d77c2008-03-05 14:28:32 -0500665static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 &cpuinfo_min_freq.attr,
667 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100668 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 &scaling_min_freq.attr,
670 &scaling_max_freq.attr,
671 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700672 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 &scaling_governor.attr,
674 &scaling_driver.attr,
675 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700676 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 NULL
678};
679
Dave Jones29464f22009-01-18 01:37:11 -0500680#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
681#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Dave Jones29464f22009-01-18 01:37:11 -0500683static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Dave Jones905d77c2008-03-05 14:28:32 -0500685 struct cpufreq_policy *policy = to_policy(kobj);
686 struct freq_attr *fattr = to_attr(attr);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500687 ssize_t ret = -EINVAL;
Stephen Boyda9144432012-07-20 18:14:38 +0000688 policy = cpufreq_cpu_get_sysfs(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (!policy)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500690 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800691
692 if (lock_policy_rwsem_read(policy->cpu) < 0)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500693 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800694
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530695 if (fattr->show)
696 ret = fattr->show(policy, buf);
697 else
698 ret = -EIO;
699
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800700 unlock_policy_rwsem_read(policy->cpu);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500701fail:
Stephen Boyda9144432012-07-20 18:14:38 +0000702 cpufreq_cpu_put_sysfs(policy);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500703no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return ret;
705}
706
Dave Jones905d77c2008-03-05 14:28:32 -0500707static ssize_t store(struct kobject *kobj, struct attribute *attr,
708 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Dave Jones905d77c2008-03-05 14:28:32 -0500710 struct cpufreq_policy *policy = to_policy(kobj);
711 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500712 ssize_t ret = -EINVAL;
Stephen Boyda9144432012-07-20 18:14:38 +0000713 policy = cpufreq_cpu_get_sysfs(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (!policy)
Dave Jonesa07530b2008-03-05 14:22:25 -0500715 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800716
717 if (lock_policy_rwsem_write(policy->cpu) < 0)
Dave Jonesa07530b2008-03-05 14:22:25 -0500718 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800719
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530720 if (fattr->store)
721 ret = fattr->store(policy, buf, count);
722 else
723 ret = -EIO;
724
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800725 unlock_policy_rwsem_write(policy->cpu);
Dave Jonesa07530b2008-03-05 14:22:25 -0500726fail:
Stephen Boyda9144432012-07-20 18:14:38 +0000727 cpufreq_cpu_put_sysfs(policy);
Dave Jonesa07530b2008-03-05 14:22:25 -0500728no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 return ret;
730}
731
Dave Jones905d77c2008-03-05 14:28:32 -0500732static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
Dave Jones905d77c2008-03-05 14:28:32 -0500734 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200735 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 complete(&policy->kobj_unregister);
737}
738
Emese Revfy52cf25d2010-01-19 02:58:23 +0100739static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 .show = show,
741 .store = store,
742};
743
744static struct kobj_type ktype_cpufreq = {
745 .sysfs_ops = &sysfs_ops,
746 .default_attrs = default_attrs,
747 .release = cpufreq_sysfs_release,
748};
749
Viresh Kumar2361be22013-05-17 16:09:09 +0530750struct kobject *cpufreq_global_kobject;
751EXPORT_SYMBOL(cpufreq_global_kobject);
752
753static int cpufreq_global_kobject_usage;
754
755int cpufreq_get_global_kobject(void)
756{
757 if (!cpufreq_global_kobject_usage++)
758 return kobject_add(cpufreq_global_kobject,
759 &cpu_subsys.dev_root->kobj, "%s", "cpufreq");
760
761 return 0;
762}
763EXPORT_SYMBOL(cpufreq_get_global_kobject);
764
765void cpufreq_put_global_kobject(void)
766{
767 if (!--cpufreq_global_kobject_usage)
768 kobject_del(cpufreq_global_kobject);
769}
770EXPORT_SYMBOL(cpufreq_put_global_kobject);
771
772int cpufreq_sysfs_create_file(const struct attribute *attr)
773{
774 int ret = cpufreq_get_global_kobject();
775
776 if (!ret) {
777 ret = sysfs_create_file(cpufreq_global_kobject, attr);
778 if (ret)
779 cpufreq_put_global_kobject();
780 }
781
782 return ret;
783}
784EXPORT_SYMBOL(cpufreq_sysfs_create_file);
785
786void cpufreq_sysfs_remove_file(const struct attribute *attr)
787{
788 sysfs_remove_file(cpufreq_global_kobject, attr);
789 cpufreq_put_global_kobject();
790}
791EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
792
Dave Jones19d6f7e2009-07-08 17:35:39 -0400793/* symlink affected CPUs */
Alex Chiangcf3289d02009-11-17 20:27:08 -0700794static int cpufreq_add_dev_symlink(unsigned int cpu,
795 struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400796{
797 unsigned int j;
798 int ret = 0;
799
800 for_each_cpu(j, policy->cpus) {
801 struct cpufreq_policy *managed_policy;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800802 struct device *cpu_dev;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400803
804 if (j == cpu)
805 continue;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400806
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200807 pr_debug("CPU %u already managed, adding link\n", j);
Dave Jones19d6f7e2009-07-08 17:35:39 -0400808 managed_policy = cpufreq_cpu_get(cpu);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800809 cpu_dev = get_cpu_device(j);
810 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Dave Jones19d6f7e2009-07-08 17:35:39 -0400811 "cpufreq");
812 if (ret) {
813 cpufreq_cpu_put(managed_policy);
814 return ret;
815 }
816 }
817 return ret;
818}
819
Alex Chiangcf3289d02009-11-17 20:27:08 -0700820static int cpufreq_add_dev_interface(unsigned int cpu,
821 struct cpufreq_policy *policy,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800822 struct device *dev)
Dave Jones909a6942009-07-08 18:05:42 -0400823{
Dave Jonesecf7e462009-07-08 18:48:47 -0400824 struct cpufreq_policy new_policy;
Dave Jones909a6942009-07-08 18:05:42 -0400825 struct freq_attr **drv_attr;
826 unsigned long flags;
827 int ret = 0;
828 unsigned int j;
829
830 /* prepare interface data */
831 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800832 &dev->kobj, "cpufreq");
Dave Jones909a6942009-07-08 18:05:42 -0400833 if (ret)
834 return ret;
835
836 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200837 drv_attr = cpufreq_driver->attr;
Dave Jones909a6942009-07-08 18:05:42 -0400838 while ((drv_attr) && (*drv_attr)) {
839 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
840 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200841 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400842 drv_attr++;
843 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200844 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -0400845 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
846 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200847 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400848 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200849 if (cpufreq_driver->target) {
Dave Jones909a6942009-07-08 18:05:42 -0400850 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.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 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200854 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +0100855 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
856 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200857 goto err_out_kobj_put;
Thomas Renningere2f74f32009-11-19 12:31:01 +0100858 }
Dave Jones909a6942009-07-08 18:05:42 -0400859
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000860 write_lock_irqsave(&cpufreq_driver_lock, flags);
Dave Jones909a6942009-07-08 18:05:42 -0400861 for_each_cpu(j, policy->cpus) {
Dave Jones909a6942009-07-08 18:05:42 -0400862 per_cpu(cpufreq_cpu_data, j) = policy;
Tejun Heof1625062009-10-29 22:34:13 +0900863 per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
Dave Jones909a6942009-07-08 18:05:42 -0400864 }
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000865 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones909a6942009-07-08 18:05:42 -0400866
867 ret = cpufreq_add_dev_symlink(cpu, policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400868 if (ret)
869 goto err_out_kobj_put;
870
871 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
872 /* assure that the starting sequence is run in __cpufreq_set_policy */
873 policy->governor = NULL;
874
875 /* set default policy */
876 ret = __cpufreq_set_policy(policy, &new_policy);
877 policy->user_policy.policy = policy->policy;
878 policy->user_policy.governor = policy->governor;
879
880 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200881 pr_debug("setting policy failed\n");
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200882 if (cpufreq_driver->exit)
883 cpufreq_driver->exit(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400884 }
Dave Jones909a6942009-07-08 18:05:42 -0400885 return ret;
886
887err_out_kobj_put:
888 kobject_put(&policy->kobj);
889 wait_for_completion(&policy->kobj_unregister);
890 return ret;
891}
892
Viresh Kumarfcf80582013-01-29 14:39:08 +0000893#ifdef CONFIG_HOTPLUG_CPU
894static int cpufreq_add_policy_cpu(unsigned int cpu, unsigned int sibling,
895 struct device *dev)
896{
897 struct cpufreq_policy *policy;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200898 int ret = 0, has_target = !!cpufreq_driver->target;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000899 unsigned long flags;
900
901 policy = cpufreq_cpu_get(sibling);
902 WARN_ON(!policy);
903
Viresh Kumar820c6ca2013-04-22 00:48:03 +0200904 if (has_target)
905 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000906
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530907 lock_policy_rwsem_write(sibling);
908
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000909 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530910
Viresh Kumarfcf80582013-01-29 14:39:08 +0000911 cpumask_set_cpu(cpu, policy->cpus);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530912 per_cpu(cpufreq_policy_cpu, cpu) = policy->cpu;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000913 per_cpu(cpufreq_cpu_data, cpu) = policy;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000914 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000915
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530916 unlock_policy_rwsem_write(sibling);
917
Viresh Kumar820c6ca2013-04-22 00:48:03 +0200918 if (has_target) {
919 __cpufreq_governor(policy, CPUFREQ_GOV_START);
920 __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
921 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000922
Viresh Kumarfcf80582013-01-29 14:39:08 +0000923 ret = sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
924 if (ret) {
925 cpufreq_cpu_put(policy);
926 return ret;
927 }
928
929 return 0;
930}
931#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933/**
934 * cpufreq_add_dev - add a CPU device
935 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500936 * Adds the cpufreq interface for a CPU device.
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400937 *
938 * The Oracle says: try running cpufreq registration/unregistration concurrently
939 * with with cpu hotplugging and all hell will break loose. Tried to clean this
940 * mess up, but more thorough testing is needed. - Mathieu
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800942static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
Viresh Kumarfcf80582013-01-29 14:39:08 +0000944 unsigned int j, cpu = dev->id;
Viresh Kumar65922462013-02-07 10:56:03 +0530945 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 unsigned long flags;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500948#ifdef CONFIG_HOTPLUG_CPU
Viresh Kumarfcf80582013-01-29 14:39:08 +0000949 struct cpufreq_governor *gov;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500950 int sibling;
951#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Ashok Rajc32b6b82005-10-30 14:59:54 -0800953 if (cpu_is_offline(cpu))
954 return 0;
955
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200956 pr_debug("adding CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958#ifdef CONFIG_SMP
959 /* check whether a different CPU already registered this
960 * CPU because it is in the same boat. */
961 policy = cpufreq_cpu_get(cpu);
962 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -0500963 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return 0;
965 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000966
967#ifdef CONFIG_HOTPLUG_CPU
968 /* Check if this cpu was hot-unplugged earlier and has siblings */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000969 read_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000970 for_each_online_cpu(sibling) {
971 struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530972 if (cp && cpumask_test_cpu(cpu, cp->related_cpus)) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000973 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000974 return cpufreq_add_policy_cpu(cpu, sibling, dev);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530975 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000976 }
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000977 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000978#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979#endif
980
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200981 if (!try_module_get(cpufreq_driver->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 ret = -EINVAL;
983 goto module_out;
984 }
985
Dave Jonese98df502005-10-20 15:17:43 -0700986 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
Dave Jones059019a2009-07-08 16:30:03 -0400987 if (!policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 goto nomem_out;
Dave Jones059019a2009-07-08 16:30:03 -0400989
990 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400991 goto err_free_policy;
Dave Jones059019a2009-07-08 16:30:03 -0400992
993 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400994 goto err_free_cpumask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 policy->cpu = cpu;
Viresh Kumar65922462013-02-07 10:56:03 +0530997 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Rusty Russell835481d2009-01-04 05:18:06 -0800998 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001000 /* Initially set CPU itself as the policy_cpu */
Tejun Heof1625062009-10-29 22:34:13 +09001001 per_cpu(cpufreq_policy_cpu, cpu) = cpu;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 init_completion(&policy->kobj_unregister);
David Howells65f27f32006-11-22 14:55:48 +00001004 INIT_WORK(&policy->update, handle_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 /* call driver. From then on the cpufreq must be able
1007 * to accept all calls to ->verify and ->setpolicy for this CPU
1008 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001009 ret = cpufreq_driver->init(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001011 pr_debug("initialization failed\n");
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301012 goto err_set_policy_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001014
Viresh Kumarfcf80582013-01-29 14:39:08 +00001015 /* related cpus should atleast have policy->cpus */
1016 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
1017
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001018 /*
1019 * affected cpus must always be the one, which are online. We aren't
1020 * managing offline cpus here.
1021 */
1022 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1023
Mike Chan187d9f42008-12-04 12:19:17 -08001024 policy->user_policy.min = policy->min;
1025 policy->user_policy.max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Thomas Renningera1531ac2008-07-29 22:32:58 -07001027 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1028 CPUFREQ_START, policy);
1029
Viresh Kumarfcf80582013-01-29 14:39:08 +00001030#ifdef CONFIG_HOTPLUG_CPU
1031 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
1032 if (gov) {
1033 policy->governor = gov;
1034 pr_debug("Restoring governor %s for cpu %d\n",
1035 policy->governor->name, cpu);
Thomas Renninger4bfa0422009-07-24 15:25:03 +02001036 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001037#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001039 ret = cpufreq_add_dev_interface(cpu, policy, dev);
Dave Jones19d6f7e2009-07-08 17:35:39 -04001040 if (ret)
1041 goto err_out_unregister;
Dave Jones8ff69732006-03-05 03:37:23 -05001042
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001043 kobject_uevent(&policy->kobj, KOBJ_ADD);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001044 module_put(cpufreq_driver->owner);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001045 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return 0;
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049err_out_unregister:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001050 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rusty Russell835481d2009-01-04 05:18:06 -08001051 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -07001052 per_cpu(cpufreq_cpu_data, j) = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001053 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -08001055 kobject_put(&policy->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 wait_for_completion(&policy->kobj_unregister);
1057
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301058err_set_policy_cpu:
1059 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Xiaotian Fengcad70a62010-07-20 20:11:02 +08001060 free_cpumask_var(policy->related_cpus);
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001061err_free_cpumask:
1062 free_cpumask_var(policy->cpus);
1063err_free_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 kfree(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065nomem_out:
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001066 module_put(cpufreq_driver->owner);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001067module_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 return ret;
1069}
1070
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001071static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
1072{
1073 int j;
1074
1075 policy->last_cpu = policy->cpu;
1076 policy->cpu = cpu;
1077
Viresh Kumar3361b7b2013-02-04 11:38:51 +00001078 for_each_cpu(j, policy->cpus)
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001079 per_cpu(cpufreq_policy_cpu, j) = cpu;
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001080
1081#ifdef CONFIG_CPU_FREQ_TABLE
1082 cpufreq_frequency_table_update_policy_cpu(policy);
1083#endif
1084 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1085 CPUFREQ_UPDATE_POLICY_CPU, policy);
1086}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088/**
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001089 * __cpufreq_remove_dev - remove a CPU device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 *
1091 * Removes the cpufreq interface for a CPU device.
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001092 * Caller should already have policy_rwsem in write mode for this CPU.
1093 * This routine frees the rwsem before returning.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 */
Viresh Kumarbb176f72013-06-19 14:19:33 +05301095static int __cpufreq_remove_dev(struct device *dev,
1096 struct subsys_interface *sif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001098 unsigned int cpu = dev->id, ret, cpus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 unsigned long flags;
1100 struct cpufreq_policy *data;
Amerigo Wang499bca92010-03-04 03:23:46 -05001101 struct kobject *kobj;
1102 struct completion *cmp;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001103 struct device *cpu_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001105 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001107 write_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 data = per_cpu(cpufreq_cpu_data, cpu);
Mike Travis7a6aedf2008-03-25 15:06:53 -07001110 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001112 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (!data) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001115 pr_debug("%s: No cpu_data found\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001119 if (cpufreq_driver->target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001121
Jacob Shin27ecddc2011-04-27 13:32:11 -05001122#ifdef CONFIG_HOTPLUG_CPU
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001123 if (!cpufreq_driver->setpolicy)
Dirk Brandewiefa69e332013-02-06 09:02:11 -08001124 strncpy(per_cpu(cpufreq_cpu_governor, cpu),
1125 data->governor->name, CPUFREQ_NAME_LEN);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001126#endif
1127
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301128 WARN_ON(lock_policy_rwsem_write(cpu));
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001129 cpus = cpumask_weight(data->cpus);
Viresh Kumare4969eb2013-04-11 08:04:53 +00001130
1131 if (cpus > 1)
1132 cpumask_clear_cpu(cpu, data->cpus);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301133 unlock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Viresh Kumar73bf0fc2013-02-05 22:21:14 +01001135 if (cpu != data->cpu) {
1136 sysfs_remove_link(&dev->kobj, "cpufreq");
1137 } else if (cpus > 1) {
Venki Pallipadiec282972007-03-26 12:03:19 -07001138 /* first sibling now owns the new sysfs dir */
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001139 cpu_dev = get_cpu_device(cpumask_first(data->cpus));
1140 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
1141 ret = kobject_move(&data->kobj, &cpu_dev->kobj);
1142 if (ret) {
1143 pr_err("%s: Failed to move kobj: %d", __func__, ret);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301144
1145 WARN_ON(lock_policy_rwsem_write(cpu));
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001146 cpumask_set_cpu(cpu, data->cpus);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301147
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001148 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301149 per_cpu(cpufreq_cpu_data, cpu) = data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001150 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301151
1152 unlock_policy_rwsem_write(cpu);
1153
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001154 ret = sysfs_create_link(&cpu_dev->kobj, &data->kobj,
1155 "cpufreq");
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001156 return -EINVAL;
1157 }
Venki Pallipadiec282972007-03-26 12:03:19 -07001158
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301159 WARN_ON(lock_policy_rwsem_write(cpu));
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001160 update_policy_cpu(data, cpu_dev->id);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301161 unlock_policy_rwsem_write(cpu);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001162 pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
1163 __func__, cpu_dev->id, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001164 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001165
Viresh Kumard96038e2013-04-30 14:32:18 +00001166 if ((cpus == 1) && (cpufreq_driver->target))
1167 __cpufreq_governor(data, CPUFREQ_GOV_POLICY_EXIT);
1168
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001169 pr_debug("%s: removing link, cpu: %d\n", __func__, cpu);
1170 cpufreq_cpu_put(data);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001171
1172 /* If cpu is last user of policy, free policy */
1173 if (cpus == 1) {
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301174 lock_policy_rwsem_read(cpu);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001175 kobj = &data->kobj;
1176 cmp = &data->kobj_unregister;
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301177 unlock_policy_rwsem_read(cpu);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001178 kobject_put(kobj);
1179
1180 /* we need to make sure that the underlying kobj is actually
1181 * not referenced anymore by anybody before we proceed with
1182 * unloading.
1183 */
1184 pr_debug("waiting for dropping of refcount\n");
1185 wait_for_completion(cmp);
1186 pr_debug("wait complete\n");
1187
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001188 if (cpufreq_driver->exit)
1189 cpufreq_driver->exit(data);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001190
1191 free_cpumask_var(data->related_cpus);
1192 free_cpumask_var(data->cpus);
1193 kfree(data);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001194 } else if (cpufreq_driver->target) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001195 __cpufreq_governor(data, CPUFREQ_GOV_START);
1196 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301199 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 return 0;
1201}
1202
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001203static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001204{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001205 unsigned int cpu = dev->id;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001206 int retval;
1207
1208 if (cpu_is_offline(cpu))
1209 return 0;
1210
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001211 retval = __cpufreq_remove_dev(dev, sif);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001212 return retval;
1213}
1214
David Howells65f27f32006-11-22 14:55:48 +00001215static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
David Howells65f27f32006-11-22 14:55:48 +00001217 struct cpufreq_policy *policy =
1218 container_of(work, struct cpufreq_policy, update);
1219 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001220 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 cpufreq_update_policy(cpu);
1222}
1223
1224/**
Viresh Kumarbb176f72013-06-19 14:19:33 +05301225 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1226 * in deep trouble.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 * @cpu: cpu number
1228 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1229 * @new_freq: CPU frequency the CPU actually runs at
1230 *
Dave Jones29464f22009-01-18 01:37:11 -05001231 * We adjust to current frequency first, and need to clean up later.
1232 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301234static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1235 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301237 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301239 unsigned long flags;
1240
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001241 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 freqs.old = old_freq;
1245 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301246
1247 read_lock_irqsave(&cpufreq_driver_lock, flags);
1248 policy = per_cpu(cpufreq_cpu_data, cpu);
1249 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1250
1251 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1252 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253}
1254
Dave Jones32ee8c32006-02-28 00:43:23 -05001255/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301256 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001257 * @cpu: CPU number
1258 *
1259 * This is the last known freq, without actually getting it from the driver.
1260 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1261 */
1262unsigned int cpufreq_quick_get(unsigned int cpu)
1263{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001264 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301265 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001266
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001267 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1268 return cpufreq_driver->get(cpu);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001269
1270 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001271 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301272 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001273 cpufreq_cpu_put(policy);
1274 }
1275
Dave Jones4d34a672008-02-07 16:33:49 -05001276 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001277}
1278EXPORT_SYMBOL(cpufreq_quick_get);
1279
Jesse Barnes3d737102011-06-28 10:59:12 -07001280/**
1281 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1282 * @cpu: CPU number
1283 *
1284 * Just return the max possible frequency for a given CPU.
1285 */
1286unsigned int cpufreq_quick_get_max(unsigned int cpu)
1287{
1288 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1289 unsigned int ret_freq = 0;
1290
1291 if (policy) {
1292 ret_freq = policy->max;
1293 cpufreq_cpu_put(policy);
1294 }
1295
1296 return ret_freq;
1297}
1298EXPORT_SYMBOL(cpufreq_quick_get_max);
1299
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001300static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001302 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301303 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001305 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001306 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001308 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301310 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001311 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301312 /* verify no discrepancy between actual and
1313 saved value exists */
1314 if (unlikely(ret_freq != policy->cur)) {
1315 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 schedule_work(&policy->update);
1317 }
1318 }
1319
Dave Jones4d34a672008-02-07 16:33:49 -05001320 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001321}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001323/**
1324 * cpufreq_get - get the current CPU frequency (in kHz)
1325 * @cpu: CPU number
1326 *
1327 * Get the CPU current (static) CPU frequency
1328 */
1329unsigned int cpufreq_get(unsigned int cpu)
1330{
1331 unsigned int ret_freq = 0;
1332 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1333
1334 if (!policy)
1335 goto out;
1336
1337 if (unlikely(lock_policy_rwsem_read(cpu)))
1338 goto out_policy;
1339
1340 ret_freq = __cpufreq_get(cpu);
1341
1342 unlock_policy_rwsem_read(cpu);
1343
1344out_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 cpufreq_cpu_put(policy);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001346out:
Dave Jones4d34a672008-02-07 16:33:49 -05001347 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348}
1349EXPORT_SYMBOL(cpufreq_get);
1350
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001351static struct subsys_interface cpufreq_interface = {
1352 .name = "cpufreq",
1353 .subsys = &cpu_subsys,
1354 .add_dev = cpufreq_add_dev,
1355 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001356};
1357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001359 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1360 *
1361 * This function is only executed for the boot processor. The other CPUs
1362 * have been put offline by means of CPU hotplug.
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001363 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001364static int cpufreq_bp_suspend(void)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001365{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301366 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001367
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001368 int cpu = smp_processor_id();
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001369 struct cpufreq_policy *cpu_policy;
1370
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001371 pr_debug("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001372
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001373 /* If there's no policy for the boot CPU, we have nothing to do. */
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001374 cpu_policy = cpufreq_cpu_get(cpu);
1375 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001376 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001377
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001378 if (cpufreq_driver->suspend) {
1379 ret = cpufreq_driver->suspend(cpu_policy);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001380 if (ret)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001381 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1382 "step on CPU %u\n", cpu_policy->cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001383 }
1384
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001385 cpufreq_cpu_put(cpu_policy);
Dave Jonesc9060492008-02-07 16:32:18 -05001386 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001387}
1388
1389/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001390 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 *
1392 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001393 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1394 * restored. It will verify that the current freq is in sync with
1395 * what we believe it to be. This is a bit later than when it
1396 * should be, but nonethteless it's better than calling
1397 * cpufreq_driver->get() here which might re-enable interrupts...
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001398 *
1399 * This function is only executed for the boot CPU. The other CPUs have not
1400 * been turned on yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001402static void cpufreq_bp_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301404 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001405
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001406 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 struct cpufreq_policy *cpu_policy;
1408
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001409 pr_debug("resuming cpu %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001411 /* If there's no policy for the boot CPU, we have nothing to do. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 cpu_policy = cpufreq_cpu_get(cpu);
1413 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001414 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001416 if (cpufreq_driver->resume) {
1417 ret = cpufreq_driver->resume(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 if (ret) {
1419 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1420 "step on CPU %u\n", cpu_policy->cpu);
Dave Jonesc9060492008-02-07 16:32:18 -05001421 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
1423 }
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 schedule_work(&cpu_policy->update);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001426
Dave Jonesc9060492008-02-07 16:32:18 -05001427fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
1430
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001431static struct syscore_ops cpufreq_syscore_ops = {
1432 .suspend = cpufreq_bp_suspend,
1433 .resume = cpufreq_bp_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434};
1435
Borislav Petkov9d950462013-01-20 10:24:28 +00001436/**
1437 * cpufreq_get_current_driver - return current driver's name
1438 *
1439 * Return the name string of the currently loaded cpufreq driver
1440 * or NULL, if none.
1441 */
1442const char *cpufreq_get_current_driver(void)
1443{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001444 if (cpufreq_driver)
1445 return cpufreq_driver->name;
1446
1447 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001448}
1449EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451/*********************************************************************
1452 * NOTIFIER LISTS INTERFACE *
1453 *********************************************************************/
1454
1455/**
1456 * cpufreq_register_notifier - register a driver with cpufreq
1457 * @nb: notifier function to register
1458 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1459 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001460 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 * are notified about clock rate changes (once before and once after
1462 * the transition), or a list of drivers that are notified about
1463 * changes in cpufreq policy.
1464 *
1465 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001466 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 */
1468int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1469{
1470 int ret;
1471
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001472 if (cpufreq_disabled())
1473 return -EINVAL;
1474
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001475 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 switch (list) {
1478 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001479 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001480 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 break;
1482 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001483 ret = blocking_notifier_chain_register(
1484 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 break;
1486 default:
1487 ret = -EINVAL;
1488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
1490 return ret;
1491}
1492EXPORT_SYMBOL(cpufreq_register_notifier);
1493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494/**
1495 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1496 * @nb: notifier block to be unregistered
Viresh Kumarbb176f72013-06-19 14:19:33 +05301497 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 *
1499 * Remove a driver from the CPU frequency notifier list.
1500 *
1501 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001502 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 */
1504int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1505{
1506 int ret;
1507
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001508 if (cpufreq_disabled())
1509 return -EINVAL;
1510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 switch (list) {
1512 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001513 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001514 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 break;
1516 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001517 ret = blocking_notifier_chain_unregister(
1518 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 break;
1520 default:
1521 ret = -EINVAL;
1522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524 return ret;
1525}
1526EXPORT_SYMBOL(cpufreq_unregister_notifier);
1527
1528
1529/*********************************************************************
1530 * GOVERNORS *
1531 *********************************************************************/
1532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533int __cpufreq_driver_target(struct cpufreq_policy *policy,
1534 unsigned int target_freq,
1535 unsigned int relation)
1536{
1537 int retval = -EINVAL;
Viresh Kumar72499242012-10-31 01:28:21 +01001538 unsigned int old_target_freq = target_freq;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001539
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001540 if (cpufreq_disabled())
1541 return -ENODEV;
1542
Viresh Kumar72499242012-10-31 01:28:21 +01001543 /* Make sure that target_freq is within supported range */
1544 if (target_freq > policy->max)
1545 target_freq = policy->max;
1546 if (target_freq < policy->min)
1547 target_freq = policy->min;
1548
1549 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1550 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001551
1552 if (target_freq == policy->cur)
1553 return 0;
1554
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001555 if (cpufreq_driver->target)
1556 retval = cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 return retval;
1559}
1560EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562int cpufreq_driver_target(struct cpufreq_policy *policy,
1563 unsigned int target_freq,
1564 unsigned int relation)
1565{
Julia Lawallf1829e42008-07-25 22:44:53 +02001566 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001568 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
Julia Lawallf1829e42008-07-25 22:44:53 +02001569 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571 ret = __cpufreq_driver_target(policy, target_freq, relation);
1572
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001573 unlock_policy_rwsem_write(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Julia Lawallf1829e42008-07-25 22:44:53 +02001575fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 return ret;
1577}
1578EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1579
venkatesh.pallipadi@intel.combf0b90e2008-08-04 11:59:07 -07001580int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu)
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001581{
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001582 if (cpufreq_disabled())
Viresh Kumara262e942013-05-31 06:15:08 +00001583 return 0;
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001584
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001585 if (!cpufreq_driver->getavg)
Viresh Kumar0676f7f2012-10-24 23:39:48 +02001586 return 0;
1587
Viresh Kumara262e942013-05-31 06:15:08 +00001588 return cpufreq_driver->getavg(policy, cpu);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001589}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001590EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001591
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001592/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001593 * when "event" is CPUFREQ_GOV_LIMITS
1594 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301596static int __cpufreq_governor(struct cpufreq_policy *policy,
1597 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598{
Dave Jonescc993ca2005-07-28 09:43:56 -07001599 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001600
1601 /* Only must be defined when default governor is known to have latency
1602 restrictions, like e.g. conservative or ondemand.
1603 That this is the case is already ensured in Kconfig
1604 */
1605#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1606 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1607#else
1608 struct cpufreq_governor *gov = NULL;
1609#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001610
1611 if (policy->governor->max_transition_latency &&
1612 policy->cpuinfo.transition_latency >
1613 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07001614 if (!gov)
1615 return -EINVAL;
1616 else {
1617 printk(KERN_WARNING "%s governor failed, too long"
1618 " transition latency of HW, fallback"
1619 " to %s governor\n",
1620 policy->governor->name,
1621 gov->name);
1622 policy->governor = gov;
1623 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626 if (!try_module_get(policy->governor->owner))
1627 return -EINVAL;
1628
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001629 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301630 policy->cpu, event);
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001631
1632 mutex_lock(&cpufreq_governor_lock);
1633 if ((!policy->governor_enabled && (event == CPUFREQ_GOV_STOP)) ||
1634 (policy->governor_enabled && (event == CPUFREQ_GOV_START))) {
1635 mutex_unlock(&cpufreq_governor_lock);
1636 return -EBUSY;
1637 }
1638
1639 if (event == CPUFREQ_GOV_STOP)
1640 policy->governor_enabled = false;
1641 else if (event == CPUFREQ_GOV_START)
1642 policy->governor_enabled = true;
1643
1644 mutex_unlock(&cpufreq_governor_lock);
1645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 ret = policy->governor->governor(policy, event);
1647
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001648 if (!ret) {
1649 if (event == CPUFREQ_GOV_POLICY_INIT)
1650 policy->governor->initialized++;
1651 else if (event == CPUFREQ_GOV_POLICY_EXIT)
1652 policy->governor->initialized--;
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001653 } else {
1654 /* Restore original values */
1655 mutex_lock(&cpufreq_governor_lock);
1656 if (event == CPUFREQ_GOV_STOP)
1657 policy->governor_enabled = true;
1658 else if (event == CPUFREQ_GOV_START)
1659 policy->governor_enabled = false;
1660 mutex_unlock(&cpufreq_governor_lock);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001661 }
Viresh Kumarb3940582013-02-01 05:42:58 +00001662
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301663 /* we keep one module reference alive for
1664 each CPU governed by this CPU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 if ((event != CPUFREQ_GOV_START) || ret)
1666 module_put(policy->governor->owner);
1667 if ((event == CPUFREQ_GOV_STOP) && !ret)
1668 module_put(policy->governor->owner);
1669
1670 return ret;
1671}
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673int cpufreq_register_governor(struct cpufreq_governor *governor)
1674{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001675 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
1677 if (!governor)
1678 return -EINVAL;
1679
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001680 if (cpufreq_disabled())
1681 return -ENODEV;
1682
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001683 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001684
Viresh Kumarb3940582013-02-01 05:42:58 +00001685 governor->initialized = 0;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001686 err = -EBUSY;
1687 if (__find_governor(governor->name) == NULL) {
1688 err = 0;
1689 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Dave Jones32ee8c32006-02-28 00:43:23 -05001692 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001693 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694}
1695EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1698{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001699#ifdef CONFIG_HOTPLUG_CPU
1700 int cpu;
1701#endif
1702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 if (!governor)
1704 return;
1705
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001706 if (cpufreq_disabled())
1707 return;
1708
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001709#ifdef CONFIG_HOTPLUG_CPU
1710 for_each_present_cpu(cpu) {
1711 if (cpu_online(cpu))
1712 continue;
1713 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1714 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1715 }
1716#endif
1717
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001718 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001720 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 return;
1722}
1723EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1724
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726/*********************************************************************
1727 * POLICY INTERFACE *
1728 *********************************************************************/
1729
1730/**
1731 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05001732 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1733 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 *
1735 * Reads the current cpufreq policy.
1736 */
1737int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1738{
1739 struct cpufreq_policy *cpu_policy;
1740 if (!policy)
1741 return -EINVAL;
1742
1743 cpu_policy = cpufreq_cpu_get(cpu);
1744 if (!cpu_policy)
1745 return -EINVAL;
1746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
1749 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 return 0;
1751}
1752EXPORT_SYMBOL(cpufreq_get_policy);
1753
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001754/*
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301755 * data : current policy.
1756 * policy : policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001757 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301758static int __cpufreq_set_policy(struct cpufreq_policy *data,
1759 struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001761 int ret = 0, failed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001763 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 policy->min, policy->max);
1765
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301766 memcpy(&policy->cpuinfo, &data->cpuinfo,
1767 sizeof(struct cpufreq_cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
Yi Yang53391fa2008-01-30 13:33:34 +01001769 if (policy->min > data->max || policy->max < data->min) {
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02001770 ret = -EINVAL;
1771 goto error_out;
1772 }
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 /* verify the cpu speed can be set within this limit */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001775 ret = cpufreq_driver->verify(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 if (ret)
1777 goto error_out;
1778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001780 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1781 CPUFREQ_ADJUST, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
1783 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001784 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1785 CPUFREQ_INCOMPATIBLE, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Viresh Kumarbb176f72013-06-19 14:19:33 +05301787 /*
1788 * verify the cpu speed can be set within this limit, which might be
1789 * different to the first one
1790 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001791 ret = cpufreq_driver->verify(policy);
Alan Sterne041c682006-03-27 01:16:30 -08001792 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
1795 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001796 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1797 CPUFREQ_NOTIFY, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Dave Jones7d5e3502006-02-02 17:03:42 -05001799 data->min = policy->min;
1800 data->max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001802 pr_debug("new min and max freqs are %u - %u kHz\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301803 data->min, data->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001805 if (cpufreq_driver->setpolicy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 data->policy = policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001807 pr_debug("setting range\n");
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001808 ret = cpufreq_driver->setpolicy(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 } else {
1810 if (policy->governor != data->governor) {
1811 /* save old, working values */
1812 struct cpufreq_governor *old_gov = data->governor;
1813
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001814 pr_debug("governor switch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
1816 /* end old governor */
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001817 if (data->governor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Viresh Kumar955ef482013-05-16 05:09:58 +00001819 unlock_policy_rwsem_write(policy->cpu);
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001820 __cpufreq_governor(data,
1821 CPUFREQ_GOV_POLICY_EXIT);
Viresh Kumar955ef482013-05-16 05:09:58 +00001822 lock_policy_rwsem_write(policy->cpu);
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
1825 /* start new governor */
1826 data->governor = policy->governor;
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001827 if (!__cpufreq_governor(data, CPUFREQ_GOV_POLICY_INIT)) {
Viresh Kumar955ef482013-05-16 05:09:58 +00001828 if (!__cpufreq_governor(data, CPUFREQ_GOV_START)) {
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001829 failed = 0;
Viresh Kumar955ef482013-05-16 05:09:58 +00001830 } else {
1831 unlock_policy_rwsem_write(policy->cpu);
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001832 __cpufreq_governor(data,
1833 CPUFREQ_GOV_POLICY_EXIT);
Viresh Kumar955ef482013-05-16 05:09:58 +00001834 lock_policy_rwsem_write(policy->cpu);
1835 }
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001836 }
1837
1838 if (failed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 /* new governor failed, so re-start old one */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001840 pr_debug("starting governor %s failed\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301841 data->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 if (old_gov) {
1843 data->governor = old_gov;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301844 __cpufreq_governor(data,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001845 CPUFREQ_GOV_POLICY_INIT);
1846 __cpufreq_governor(data,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301847 CPUFREQ_GOV_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 }
1849 ret = -EINVAL;
1850 goto error_out;
1851 }
1852 /* might be a policy change, too, so fall through */
1853 }
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001854 pr_debug("governor: change or update limits\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1856 }
1857
Dave Jones7d5e3502006-02-02 17:03:42 -05001858error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 return ret;
1860}
1861
1862/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1864 * @cpu: CPU which shall be re-evaluated
1865 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001866 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 * at different times.
1868 */
1869int cpufreq_update_policy(unsigned int cpu)
1870{
1871 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1872 struct cpufreq_policy policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02001873 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Julia Lawallf1829e42008-07-25 22:44:53 +02001875 if (!data) {
1876 ret = -ENODEV;
1877 goto no_policy;
1878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Julia Lawallf1829e42008-07-25 22:44:53 +02001880 if (unlikely(lock_policy_rwsem_write(cpu))) {
1881 ret = -EINVAL;
1882 goto fail;
1883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001885 pr_debug("updating policy for CPU %u\n", cpu);
Dave Jones7d5e3502006-02-02 17:03:42 -05001886 memcpy(&policy, data, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 policy.min = data->user_policy.min;
1888 policy.max = data->user_policy.max;
1889 policy.policy = data->user_policy.policy;
1890 policy.governor = data->user_policy.governor;
1891
Viresh Kumarbb176f72013-06-19 14:19:33 +05301892 /*
1893 * BIOS might change freq behind our back
1894 * -> ask driver for current freq and notify governors about a change
1895 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001896 if (cpufreq_driver->get) {
1897 policy.cur = cpufreq_driver->get(cpu);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001898 if (!data->cur) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001899 pr_debug("Driver did not initialize current freq");
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001900 data->cur = policy.cur;
1901 } else {
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001902 if (data->cur != policy.cur && cpufreq_driver->target)
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301903 cpufreq_out_of_sync(cpu, data->cur,
1904 policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001905 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01001906 }
1907
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 ret = __cpufreq_set_policy(data, &policy);
1909
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001910 unlock_policy_rwsem_write(cpu);
1911
Julia Lawallf1829e42008-07-25 22:44:53 +02001912fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 cpufreq_cpu_put(data);
Julia Lawallf1829e42008-07-25 22:44:53 +02001914no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 return ret;
1916}
1917EXPORT_SYMBOL(cpufreq_update_policy);
1918
Satyam Sharmadd184a02007-10-02 13:28:14 -07001919static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08001920 unsigned long action, void *hcpu)
1921{
1922 unsigned int cpu = (unsigned long)hcpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001923 struct device *dev;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001924
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001925 dev = get_cpu_device(cpu);
1926 if (dev) {
Ashok Rajc32b6b82005-10-30 14:59:54 -08001927 switch (action) {
1928 case CPU_ONLINE:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001929 cpufreq_add_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001930 break;
1931 case CPU_DOWN_PREPARE:
Srivatsa S. Bhata66b2e52013-05-15 21:47:17 +02001932 case CPU_UP_CANCELED_FROZEN:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001933 __cpufreq_remove_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001934 break;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001935 case CPU_DOWN_FAILED:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001936 cpufreq_add_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001937 break;
1938 }
1939 }
1940 return NOTIFY_OK;
1941}
1942
Neal Buckendahl9c36f742010-06-22 22:02:44 -05001943static struct notifier_block __refdata cpufreq_cpu_notifier = {
Viresh Kumarbb176f72013-06-19 14:19:33 +05301944 .notifier_call = cpufreq_cpu_callback,
Ashok Rajc32b6b82005-10-30 14:59:54 -08001945};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
1947/*********************************************************************
1948 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1949 *********************************************************************/
1950
1951/**
1952 * cpufreq_register_driver - register a CPU Frequency driver
1953 * @driver_data: A struct cpufreq_driver containing the values#
1954 * submitted by the CPU Frequency driver.
1955 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05301956 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05001958 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 *
1960 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001961int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
1963 unsigned long flags;
1964 int ret;
1965
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001966 if (cpufreq_disabled())
1967 return -ENODEV;
1968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 if (!driver_data || !driver_data->verify || !driver_data->init ||
1970 ((!driver_data->setpolicy) && (!driver_data->target)))
1971 return -EINVAL;
1972
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001973 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975 if (driver_data->setpolicy)
1976 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1977
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001978 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001979 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001980 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 return -EBUSY;
1982 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001983 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001984 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001986 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001987 if (ret)
1988 goto err_null_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001990 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 int i;
1992 ret = -ENODEV;
1993
1994 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07001995 for (i = 0; i < nr_cpu_ids; i++)
1996 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07001998 break;
1999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
2001 /* if all ->init() calls failed, unregister */
2002 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002003 pr_debug("no CPU initialized for driver %s\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05302004 driver_data->name);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002005 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 }
2007 }
2008
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002009 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002010 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002012 return 0;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002013err_if_unreg:
2014 subsys_interface_unregister(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002015err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002016 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002017 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002018 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05002019 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020}
2021EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023/**
2024 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2025 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302026 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 * the right to do so, i.e. if you have succeeded in initialising before!
2028 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2029 * currently not initialised.
2030 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002031int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
2033 unsigned long flags;
2034
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002035 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002038 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002040 subsys_interface_unregister(&cpufreq_interface);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07002041 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002043 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002044 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002045 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
2047 return 0;
2048}
2049EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002050
2051static int __init cpufreq_core_init(void)
2052{
2053 int cpu;
2054
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002055 if (cpufreq_disabled())
2056 return -ENODEV;
2057
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002058 for_each_possible_cpu(cpu) {
Tejun Heof1625062009-10-29 22:34:13 +09002059 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002060 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
2061 }
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002062
Viresh Kumar2361be22013-05-17 16:09:09 +05302063 cpufreq_global_kobject = kobject_create();
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002064 BUG_ON(!cpufreq_global_kobject);
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01002065 register_syscore_ops(&cpufreq_syscore_ops);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002066
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002067 return 0;
2068}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002069core_initcall(cpufreq_core_init);