blob: 576b312645a5d050798cc44dbb6379b11a0a51d3 [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);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +053047static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data_fallback);
Viresh Kumarbb176f72013-06-19 14:19:33 +053048static DEFINE_RWLOCK(cpufreq_driver_lock);
49static DEFINE_MUTEX(cpufreq_governor_lock);
50
Thomas Renninger084f3492007-07-09 11:35:28 -070051#ifdef CONFIG_HOTPLUG_CPU
52/* This one keeps track of the previously set governor of a removed CPU */
Dmitry Monakhove77b89f2009-10-05 00:38:55 +040053static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
Thomas Renninger084f3492007-07-09 11:35:28 -070054#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080056/*
57 * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
58 * all cpufreq/hotplug/workqueue/etc related lock issues.
59 *
60 * The rules for this semaphore:
61 * - Any routine that wants to read from the policy structure will
62 * do a down_read on this semaphore.
63 * - Any routine that will write to the policy structure and/or may take away
64 * the policy altogether (eg. CPU hotplug), will hold this lock in write
65 * mode before doing so.
66 *
67 * Additional rules:
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080068 * - Governor routines that can be called in cpufreq hotplug path should not
69 * take this sem as top level hotplug notifier handler takes this.
Mathieu Desnoyers395913d2009-06-08 13:17:31 -040070 * - Lock should not be held across
71 * __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080072 */
Tejun Heof1625062009-10-29 22:34:13 +090073static DEFINE_PER_CPU(int, cpufreq_policy_cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080074static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
75
76#define lock_policy_rwsem(mode, cpu) \
Viresh Kumarfa1d8af2013-02-07 15:38:42 +053077static int lock_policy_rwsem_##mode(int cpu) \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080078{ \
Tejun Heof1625062009-10-29 22:34:13 +090079 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080080 BUG_ON(policy_cpu == -1); \
81 down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080082 \
83 return 0; \
84}
85
86lock_policy_rwsem(read, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080087lock_policy_rwsem(write, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080088
Viresh Kumarfa1d8af2013-02-07 15:38:42 +053089#define unlock_policy_rwsem(mode, cpu) \
90static void unlock_policy_rwsem_##mode(int cpu) \
91{ \
92 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
93 BUG_ON(policy_cpu == -1); \
94 up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080095}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080096
Viresh Kumarfa1d8af2013-02-07 15:38:42 +053097unlock_policy_rwsem(read, cpu);
98unlock_policy_rwsem(write, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/* internal prototypes */
Dave Jones29464f22009-01-18 01:37:11 -0500101static int __cpufreq_governor(struct cpufreq_policy *policy,
102 unsigned int event);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800103static unsigned int __cpufreq_get(unsigned int cpu);
David Howells65f27f32006-11-22 14:55:48 +0000104static void handle_update(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106/**
Dave Jones32ee8c32006-02-28 00:43:23 -0500107 * Two notifier lists: the "policy" list is involved in the
108 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 * "transition" list for kernel code that needs to handle
110 * changes to devices when the CPU clock speed changes.
111 * The mutex locks both lists.
112 */
Alan Sterne041c682006-03-27 01:16:30 -0800113static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700114static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200116static bool init_cpufreq_transition_notifier_list_called;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700117static int __init init_cpufreq_transition_notifier_list(void)
118{
119 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200120 init_cpufreq_transition_notifier_list_called = true;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700121 return 0;
122}
Linus Torvaldsb3438f82006-11-20 11:47:18 -0800123pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400125static int off __read_mostly;
Viresh Kumarda584452012-10-26 00:51:32 +0200126static int cpufreq_disabled(void)
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400127{
128 return off;
129}
130void disable_cpufreq(void)
131{
132 off = 1;
133}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134static LIST_HEAD(cpufreq_governor_list);
Dave Jones29464f22009-01-18 01:37:11 -0500135static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000137bool have_governor_per_policy(void)
138{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200139 return cpufreq_driver->have_governor_per_policy;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000140}
Viresh Kumar3f869d62013-05-16 05:09:56 +0000141EXPORT_SYMBOL_GPL(have_governor_per_policy);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000142
Viresh Kumar944e9a02013-05-16 05:09:57 +0000143struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
144{
145 if (have_governor_per_policy())
146 return &policy->kobj;
147 else
148 return cpufreq_global_kobject;
149}
150EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
151
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000152static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
153{
154 u64 idle_time;
155 u64 cur_wall_time;
156 u64 busy_time;
157
158 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
159
160 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
161 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
162 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
163 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
164 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
165 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
166
167 idle_time = cur_wall_time - busy_time;
168 if (wall)
169 *wall = cputime_to_usecs(cur_wall_time);
170
171 return cputime_to_usecs(idle_time);
172}
173
174u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
175{
176 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
177
178 if (idle_time == -1ULL)
179 return get_cpu_idle_time_jiffy(cpu, wall);
180 else if (!io_busy)
181 idle_time += get_cpu_iowait_time_us(cpu, wall);
182
183 return idle_time;
184}
185EXPORT_SYMBOL_GPL(get_cpu_idle_time);
186
Stephen Boyda9144432012-07-20 18:14:38 +0000187static struct cpufreq_policy *__cpufreq_cpu_get(unsigned int cpu, bool sysfs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 struct cpufreq_policy *data;
190 unsigned long flags;
191
Mike Travis7a6aedf2008-03-25 15:06:53 -0700192 if (cpu >= nr_cpu_ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 goto err_out;
194
195 /* get the cpufreq driver */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000196 read_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200198 if (!cpufreq_driver)
199 goto err_out_unlock;
200
201 if (!try_module_get(cpufreq_driver->owner))
202 goto err_out_unlock;
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 /* get the CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -0700205 data = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 if (!data)
208 goto err_out_put_module;
209
Stephen Boyda9144432012-07-20 18:14:38 +0000210 if (!sysfs && !kobject_get(&data->kobj))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 goto err_out_put_module;
212
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000213 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return data;
215
Dave Jones7d5e3502006-02-02 17:03:42 -0500216err_out_put_module:
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200217 module_put(cpufreq_driver->owner);
Nathan Zimmer58000432013-04-04 14:53:25 +0000218err_out_unlock:
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200219 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones7d5e3502006-02-02 17:03:42 -0500220err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return NULL;
222}
Stephen Boyda9144432012-07-20 18:14:38 +0000223
224struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
225{
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000226 if (cpufreq_disabled())
227 return NULL;
228
Stephen Boyda9144432012-07-20 18:14:38 +0000229 return __cpufreq_cpu_get(cpu, false);
230}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
232
Stephen Boyda9144432012-07-20 18:14:38 +0000233static struct cpufreq_policy *cpufreq_cpu_get_sysfs(unsigned int cpu)
234{
235 return __cpufreq_cpu_get(cpu, true);
236}
237
238static void __cpufreq_cpu_put(struct cpufreq_policy *data, bool sysfs)
239{
240 if (!sysfs)
241 kobject_put(&data->kobj);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200242 module_put(cpufreq_driver->owner);
Stephen Boyda9144432012-07-20 18:14:38 +0000243}
Dave Jones7d5e3502006-02-02 17:03:42 -0500244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245void cpufreq_cpu_put(struct cpufreq_policy *data)
246{
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000247 if (cpufreq_disabled())
248 return;
249
Stephen Boyda9144432012-07-20 18:14:38 +0000250 __cpufreq_cpu_put(data, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
253
Stephen Boyda9144432012-07-20 18:14:38 +0000254static void cpufreq_cpu_put_sysfs(struct cpufreq_policy *data)
255{
256 __cpufreq_cpu_put(data, true);
257}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
261 *********************************************************************/
262
263/**
264 * adjust_jiffies - adjust the system "loops_per_jiffy"
265 *
266 * This function alters the system "loops_per_jiffy" for the clock
267 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500268 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 * per-CPU loops_per_jiffy value wherever possible.
270 */
271#ifndef CONFIG_SMP
272static unsigned long l_p_j_ref;
Viresh Kumarbb176f72013-06-19 14:19:33 +0530273static unsigned int l_p_j_ref_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Arjan van de Ven858119e2006-01-14 13:20:43 -0800275static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 if (ci->flags & CPUFREQ_CONST_LOOPS)
278 return;
279
280 if (!l_p_j_ref_freq) {
281 l_p_j_ref = loops_per_jiffy;
282 l_p_j_ref_freq = ci->old;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200283 pr_debug("saving %lu as reference value for loops_per_jiffy; "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530284 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
Viresh Kumarbb176f72013-06-19 14:19:33 +0530286 if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700287 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530288 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
289 ci->new);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200290 pr_debug("scaling loops_per_jiffy to %lu "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530291 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293}
294#else
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530295static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
296{
297 return;
298}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299#endif
300
Viresh Kumar0956df9c2013-06-19 14:19:34 +0530301static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530302 struct cpufreq_freqs *freqs, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 BUG_ON(irqs_disabled());
305
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000306 if (cpufreq_disabled())
307 return;
308
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200309 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200310 pr_debug("notification %u of frequency transition to %u kHz\n",
Dave Jonese4472cb2006-01-31 15:53:55 -0800311 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 case CPUFREQ_PRECHANGE:
Viresh Kumar266c13d2013-07-02 16:36:28 +0530316 if (WARN(policy->transition_ongoing ==
317 cpumask_weight(policy->cpus),
Viresh Kumar7c30ed52013-06-19 10:16:55 +0530318 "In middle of another frequency transition\n"))
319 return;
320
Viresh Kumar266c13d2013-07-02 16:36:28 +0530321 policy->transition_ongoing++;
Viresh Kumar7c30ed52013-06-19 10:16:55 +0530322
Dave Jones32ee8c32006-02-28 00:43:23 -0500323 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800324 * which is not equal to what the cpufreq core thinks is
325 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200327 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800328 if ((policy) && (policy->cpu == freqs->cpu) &&
329 (policy->cur) && (policy->cur != freqs->old)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200330 pr_debug("Warning: CPU frequency is"
Dave Jonese4472cb2006-01-31 15:53:55 -0800331 " %u, cpufreq assumed %u kHz.\n",
332 freqs->old, policy->cur);
333 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700336 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800337 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
339 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 case CPUFREQ_POSTCHANGE:
Viresh Kumar7c30ed52013-06-19 10:16:55 +0530342 if (WARN(!policy->transition_ongoing,
343 "No frequency transition in progress\n"))
344 return;
345
Viresh Kumar266c13d2013-07-02 16:36:28 +0530346 policy->transition_ongoing--;
Viresh Kumar7c30ed52013-06-19 10:16:55 +0530347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200349 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
Thomas Renninger6f4f2722010-04-20 13:17:36 +0200350 (unsigned long)freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100351 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700352 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800353 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800354 if (likely(policy) && likely(policy->cpu == freqs->cpu))
355 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 break;
357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
Viresh Kumarbb176f72013-06-19 14:19:33 +0530359
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530360/**
361 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
362 * on frequency transition.
363 *
364 * This function calls the transition notifiers and the "adjust_jiffies"
365 * function. It is called twice on all CPU frequency changes that have
366 * external effects.
367 */
368void cpufreq_notify_transition(struct cpufreq_policy *policy,
369 struct cpufreq_freqs *freqs, unsigned int state)
370{
371 for_each_cpu(freqs->cpu, policy->cpus)
372 __cpufreq_notify_transition(policy, freqs, state);
373}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
375
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377/*********************************************************************
378 * SYSFS INTERFACE *
379 *********************************************************************/
380
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700381static struct cpufreq_governor *__find_governor(const char *str_governor)
382{
383 struct cpufreq_governor *t;
384
385 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
Dave Jones29464f22009-01-18 01:37:11 -0500386 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700387 return t;
388
389 return NULL;
390}
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392/**
393 * cpufreq_parse_governor - parse a governor string
394 */
Dave Jones905d77c2008-03-05 14:28:32 -0500395static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 struct cpufreq_governor **governor)
397{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700398 int err = -EINVAL;
399
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200400 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700401 goto out;
402
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200403 if (cpufreq_driver->setpolicy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
405 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700406 err = 0;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530407 } else if (!strnicmp(str_governor, "powersave",
408 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700410 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200412 } else if (cpufreq_driver->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700414
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800415 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700416
417 t = __find_governor(str_governor);
418
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700419 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700420 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700421
Kees Cook1a8e1462011-05-04 08:38:56 -0700422 mutex_unlock(&cpufreq_governor_mutex);
423 ret = request_module("cpufreq_%s", str_governor);
424 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700425
Kees Cook1a8e1462011-05-04 08:38:56 -0700426 if (ret == 0)
427 t = __find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700428 }
429
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700430 if (t != NULL) {
431 *governor = t;
432 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700434
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800435 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
Dave Jones29464f22009-01-18 01:37:11 -0500437out:
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700438 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530442 * cpufreq_per_cpu_attr_read() / show_##file_name() -
443 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 *
445 * Write out information from cpufreq_driver->policy[cpu]; object must be
446 * "unsigned int".
447 */
448
Dave Jones32ee8c32006-02-28 00:43:23 -0500449#define show_one(file_name, object) \
450static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500451(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500452{ \
Dave Jones29464f22009-01-18 01:37:11 -0500453 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
456show_one(cpuinfo_min_freq, cpuinfo.min_freq);
457show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100458show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459show_one(scaling_min_freq, min);
460show_one(scaling_max_freq, max);
461show_one(scaling_cur_freq, cur);
462
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530463static int __cpufreq_set_policy(struct cpufreq_policy *data,
464 struct cpufreq_policy *policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466/**
467 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
468 */
469#define store_one(file_name, object) \
470static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500471(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{ \
Jingoo Hanf55c9c22012-10-31 05:49:13 +0000473 unsigned int ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 struct cpufreq_policy new_policy; \
475 \
476 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
477 if (ret) \
478 return -EINVAL; \
479 \
Dave Jones29464f22009-01-18 01:37:11 -0500480 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (ret != 1) \
482 return -EINVAL; \
483 \
Thomas Renninger7970e082006-04-13 15:14:04 +0200484 ret = __cpufreq_set_policy(policy, &new_policy); \
485 policy->user_policy.object = policy->object; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 \
487 return ret ? ret : count; \
488}
489
Dave Jones29464f22009-01-18 01:37:11 -0500490store_one(scaling_min_freq, min);
491store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493/**
494 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
495 */
Dave Jones905d77c2008-03-05 14:28:32 -0500496static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
497 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800499 unsigned int cur_freq = __cpufreq_get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (!cur_freq)
501 return sprintf(buf, "<unknown>");
502 return sprintf(buf, "%u\n", cur_freq);
503}
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505/**
506 * show_scaling_governor - show the current policy for the specified CPU
507 */
Dave Jones905d77c2008-03-05 14:28:32 -0500508static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Dave Jones29464f22009-01-18 01:37:11 -0500510 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return sprintf(buf, "powersave\n");
512 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
513 return sprintf(buf, "performance\n");
514 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200515 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500516 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return -EINVAL;
518}
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520/**
521 * store_scaling_governor - store policy for the specified CPU
522 */
Dave Jones905d77c2008-03-05 14:28:32 -0500523static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
524 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Jingoo Hanf55c9c22012-10-31 05:49:13 +0000526 unsigned int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 char str_governor[16];
528 struct cpufreq_policy new_policy;
529
530 ret = cpufreq_get_policy(&new_policy, policy->cpu);
531 if (ret)
532 return ret;
533
Dave Jones29464f22009-01-18 01:37:11 -0500534 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (ret != 1)
536 return -EINVAL;
537
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530538 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
539 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return -EINVAL;
541
Viresh Kumarbb176f72013-06-19 14:19:33 +0530542 /*
543 * Do not use cpufreq_set_policy here or the user_policy.max
544 * will be wrongly overridden
545 */
Thomas Renninger7970e082006-04-13 15:14:04 +0200546 ret = __cpufreq_set_policy(policy, &new_policy);
547
548 policy->user_policy.policy = policy->policy;
549 policy->user_policy.governor = policy->governor;
Thomas Renninger7970e082006-04-13 15:14:04 +0200550
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530551 if (ret)
552 return ret;
553 else
554 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
557/**
558 * show_scaling_driver - show the cpufreq driver currently loaded
559 */
Dave Jones905d77c2008-03-05 14:28:32 -0500560static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200562 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
565/**
566 * show_scaling_available_governors - show the available CPUfreq governors
567 */
Dave Jones905d77c2008-03-05 14:28:32 -0500568static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
569 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
571 ssize_t i = 0;
572 struct cpufreq_governor *t;
573
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200574 if (!cpufreq_driver->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 i += sprintf(buf, "performance powersave");
576 goto out;
577 }
578
579 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
Dave Jones29464f22009-01-18 01:37:11 -0500580 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
581 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200583 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500585out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 i += sprintf(&buf[i], "\n");
587 return i;
588}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700589
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800590ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
592 ssize_t i = 0;
593 unsigned int cpu;
594
Rusty Russell835481d2009-01-04 05:18:06 -0800595 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (i)
597 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
598 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
599 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500600 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
602 i += sprintf(&buf[i], "\n");
603 return i;
604}
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800605EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700607/**
608 * show_related_cpus - show the CPUs affected by each transition even if
609 * hw coordination is in use
610 */
611static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
612{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800613 return cpufreq_show_cpus(policy->related_cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700614}
615
616/**
617 * show_affected_cpus - show the CPUs affected by each transition
618 */
619static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
620{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800621 return cpufreq_show_cpus(policy->cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700622}
623
Venki Pallipadi9e769882007-10-26 10:18:21 -0700624static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500625 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700626{
627 unsigned int freq = 0;
628 unsigned int ret;
629
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700630 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700631 return -EINVAL;
632
633 ret = sscanf(buf, "%u", &freq);
634 if (ret != 1)
635 return -EINVAL;
636
637 policy->governor->store_setspeed(policy, freq);
638
639 return count;
640}
641
642static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
643{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700644 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700645 return sprintf(buf, "<unsupported>\n");
646
647 return policy->governor->show_setspeed(policy, buf);
648}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Thomas Renningere2f74f32009-11-19 12:31:01 +0100650/**
viresh kumar8bf1ac72012-10-23 01:23:33 +0200651 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100652 */
653static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
654{
655 unsigned int limit;
656 int ret;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200657 if (cpufreq_driver->bios_limit) {
658 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
Thomas Renningere2f74f32009-11-19 12:31:01 +0100659 if (!ret)
660 return sprintf(buf, "%u\n", limit);
661 }
662 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
663}
664
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200665cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
666cpufreq_freq_attr_ro(cpuinfo_min_freq);
667cpufreq_freq_attr_ro(cpuinfo_max_freq);
668cpufreq_freq_attr_ro(cpuinfo_transition_latency);
669cpufreq_freq_attr_ro(scaling_available_governors);
670cpufreq_freq_attr_ro(scaling_driver);
671cpufreq_freq_attr_ro(scaling_cur_freq);
672cpufreq_freq_attr_ro(bios_limit);
673cpufreq_freq_attr_ro(related_cpus);
674cpufreq_freq_attr_ro(affected_cpus);
675cpufreq_freq_attr_rw(scaling_min_freq);
676cpufreq_freq_attr_rw(scaling_max_freq);
677cpufreq_freq_attr_rw(scaling_governor);
678cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Dave Jones905d77c2008-03-05 14:28:32 -0500680static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 &cpuinfo_min_freq.attr,
682 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100683 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 &scaling_min_freq.attr,
685 &scaling_max_freq.attr,
686 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700687 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 &scaling_governor.attr,
689 &scaling_driver.attr,
690 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700691 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 NULL
693};
694
Dave Jones29464f22009-01-18 01:37:11 -0500695#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
696#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Dave Jones29464f22009-01-18 01:37:11 -0500698static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Dave Jones905d77c2008-03-05 14:28:32 -0500700 struct cpufreq_policy *policy = to_policy(kobj);
701 struct freq_attr *fattr = to_attr(attr);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500702 ssize_t ret = -EINVAL;
Stephen Boyda9144432012-07-20 18:14:38 +0000703 policy = cpufreq_cpu_get_sysfs(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (!policy)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500705 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800706
707 if (lock_policy_rwsem_read(policy->cpu) < 0)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500708 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800709
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530710 if (fattr->show)
711 ret = fattr->show(policy, buf);
712 else
713 ret = -EIO;
714
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800715 unlock_policy_rwsem_read(policy->cpu);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500716fail:
Stephen Boyda9144432012-07-20 18:14:38 +0000717 cpufreq_cpu_put_sysfs(policy);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500718no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return ret;
720}
721
Dave Jones905d77c2008-03-05 14:28:32 -0500722static ssize_t store(struct kobject *kobj, struct attribute *attr,
723 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Dave Jones905d77c2008-03-05 14:28:32 -0500725 struct cpufreq_policy *policy = to_policy(kobj);
726 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500727 ssize_t ret = -EINVAL;
Stephen Boyda9144432012-07-20 18:14:38 +0000728 policy = cpufreq_cpu_get_sysfs(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (!policy)
Dave Jonesa07530b2008-03-05 14:22:25 -0500730 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800731
732 if (lock_policy_rwsem_write(policy->cpu) < 0)
Dave Jonesa07530b2008-03-05 14:22:25 -0500733 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800734
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530735 if (fattr->store)
736 ret = fattr->store(policy, buf, count);
737 else
738 ret = -EIO;
739
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800740 unlock_policy_rwsem_write(policy->cpu);
Dave Jonesa07530b2008-03-05 14:22:25 -0500741fail:
Stephen Boyda9144432012-07-20 18:14:38 +0000742 cpufreq_cpu_put_sysfs(policy);
Dave Jonesa07530b2008-03-05 14:22:25 -0500743no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return ret;
745}
746
Dave Jones905d77c2008-03-05 14:28:32 -0500747static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Dave Jones905d77c2008-03-05 14:28:32 -0500749 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200750 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 complete(&policy->kobj_unregister);
752}
753
Emese Revfy52cf25d2010-01-19 02:58:23 +0100754static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 .show = show,
756 .store = store,
757};
758
759static struct kobj_type ktype_cpufreq = {
760 .sysfs_ops = &sysfs_ops,
761 .default_attrs = default_attrs,
762 .release = cpufreq_sysfs_release,
763};
764
Viresh Kumar2361be22013-05-17 16:09:09 +0530765struct kobject *cpufreq_global_kobject;
766EXPORT_SYMBOL(cpufreq_global_kobject);
767
768static int cpufreq_global_kobject_usage;
769
770int cpufreq_get_global_kobject(void)
771{
772 if (!cpufreq_global_kobject_usage++)
773 return kobject_add(cpufreq_global_kobject,
774 &cpu_subsys.dev_root->kobj, "%s", "cpufreq");
775
776 return 0;
777}
778EXPORT_SYMBOL(cpufreq_get_global_kobject);
779
780void cpufreq_put_global_kobject(void)
781{
782 if (!--cpufreq_global_kobject_usage)
783 kobject_del(cpufreq_global_kobject);
784}
785EXPORT_SYMBOL(cpufreq_put_global_kobject);
786
787int cpufreq_sysfs_create_file(const struct attribute *attr)
788{
789 int ret = cpufreq_get_global_kobject();
790
791 if (!ret) {
792 ret = sysfs_create_file(cpufreq_global_kobject, attr);
793 if (ret)
794 cpufreq_put_global_kobject();
795 }
796
797 return ret;
798}
799EXPORT_SYMBOL(cpufreq_sysfs_create_file);
800
801void cpufreq_sysfs_remove_file(const struct attribute *attr)
802{
803 sysfs_remove_file(cpufreq_global_kobject, attr);
804 cpufreq_put_global_kobject();
805}
806EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
807
Dave Jones19d6f7e2009-07-08 17:35:39 -0400808/* symlink affected CPUs */
Viresh Kumar308b60e2013-07-31 14:35:14 +0200809static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400810{
811 unsigned int j;
812 int ret = 0;
813
814 for_each_cpu(j, policy->cpus) {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800815 struct device *cpu_dev;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400816
Viresh Kumar308b60e2013-07-31 14:35:14 +0200817 if (j == policy->cpu)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400818 continue;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400819
Viresh Kumare8fdde12013-07-31 14:31:33 +0200820 pr_debug("Adding link for CPU: %u\n", j);
Viresh Kumar308b60e2013-07-31 14:35:14 +0200821 cpufreq_cpu_get(policy->cpu);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800822 cpu_dev = get_cpu_device(j);
823 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Dave Jones19d6f7e2009-07-08 17:35:39 -0400824 "cpufreq");
825 if (ret) {
Viresh Kumare8fdde12013-07-31 14:31:33 +0200826 cpufreq_cpu_put(policy);
Dave Jones19d6f7e2009-07-08 17:35:39 -0400827 return ret;
828 }
829 }
830 return ret;
831}
832
Viresh Kumar308b60e2013-07-31 14:35:14 +0200833static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800834 struct device *dev)
Dave Jones909a6942009-07-08 18:05:42 -0400835{
836 struct freq_attr **drv_attr;
Dave Jones909a6942009-07-08 18:05:42 -0400837 int ret = 0;
Dave Jones909a6942009-07-08 18:05:42 -0400838
839 /* prepare interface data */
840 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800841 &dev->kobj, "cpufreq");
Dave Jones909a6942009-07-08 18:05:42 -0400842 if (ret)
843 return ret;
844
845 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200846 drv_attr = cpufreq_driver->attr;
Dave Jones909a6942009-07-08 18:05:42 -0400847 while ((drv_attr) && (*drv_attr)) {
848 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
849 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200850 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400851 drv_attr++;
852 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200853 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -0400854 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
855 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200856 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400857 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200858 if (cpufreq_driver->target) {
Dave Jones909a6942009-07-08 18:05:42 -0400859 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
860 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200861 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400862 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200863 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +0100864 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
865 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200866 goto err_out_kobj_put;
Thomas Renningere2f74f32009-11-19 12:31:01 +0100867 }
Dave Jones909a6942009-07-08 18:05:42 -0400868
Viresh Kumar308b60e2013-07-31 14:35:14 +0200869 ret = cpufreq_add_dev_symlink(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400870 if (ret)
871 goto err_out_kobj_put;
872
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530873 return ret;
874
875err_out_kobj_put:
876 kobject_put(&policy->kobj);
877 wait_for_completion(&policy->kobj_unregister);
878 return ret;
879}
880
881static void cpufreq_init_policy(struct cpufreq_policy *policy)
882{
883 struct cpufreq_policy new_policy;
884 int ret = 0;
885
Dave Jonesecf7e462009-07-08 18:48:47 -0400886 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
887 /* assure that the starting sequence is run in __cpufreq_set_policy */
888 policy->governor = NULL;
889
890 /* set default policy */
891 ret = __cpufreq_set_policy(policy, &new_policy);
892 policy->user_policy.policy = policy->policy;
893 policy->user_policy.governor = policy->governor;
894
895 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200896 pr_debug("setting policy failed\n");
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200897 if (cpufreq_driver->exit)
898 cpufreq_driver->exit(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400899 }
Dave Jones909a6942009-07-08 18:05:42 -0400900}
901
Viresh Kumarfcf80582013-01-29 14:39:08 +0000902#ifdef CONFIG_HOTPLUG_CPU
903static int cpufreq_add_policy_cpu(unsigned int cpu, unsigned int sibling,
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +0530904 struct device *dev, bool frozen)
Viresh Kumarfcf80582013-01-29 14:39:08 +0000905{
906 struct cpufreq_policy *policy;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200907 int ret = 0, has_target = !!cpufreq_driver->target;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000908 unsigned long flags;
909
910 policy = cpufreq_cpu_get(sibling);
911 WARN_ON(!policy);
912
Viresh Kumar820c6ca2013-04-22 00:48:03 +0200913 if (has_target)
914 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000915
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530916 lock_policy_rwsem_write(sibling);
917
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000918 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530919
Viresh Kumarfcf80582013-01-29 14:39:08 +0000920 cpumask_set_cpu(cpu, policy->cpus);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530921 per_cpu(cpufreq_policy_cpu, cpu) = policy->cpu;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000922 per_cpu(cpufreq_cpu_data, cpu) = policy;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000923 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000924
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530925 unlock_policy_rwsem_write(sibling);
926
Viresh Kumar820c6ca2013-04-22 00:48:03 +0200927 if (has_target) {
928 __cpufreq_governor(policy, CPUFREQ_GOV_START);
929 __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
930 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000931
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +0530932 /* Don't touch sysfs links during light-weight init */
933 if (frozen) {
934 /* Drop the extra refcount that we took above */
Viresh Kumarfcf80582013-01-29 14:39:08 +0000935 cpufreq_cpu_put(policy);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +0530936 return 0;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000937 }
938
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +0530939 ret = sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
940 if (ret)
941 cpufreq_cpu_put(policy);
942
943 return ret;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000944}
945#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530947static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu)
948{
949 struct cpufreq_policy *policy;
950 unsigned long flags;
951
952 write_lock_irqsave(&cpufreq_driver_lock, flags);
953
954 policy = per_cpu(cpufreq_cpu_data_fallback, cpu);
955
956 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
957
958 return policy;
959}
960
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +0530961static struct cpufreq_policy *cpufreq_policy_alloc(void)
962{
963 struct cpufreq_policy *policy;
964
965 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
966 if (!policy)
967 return NULL;
968
969 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
970 goto err_free_policy;
971
972 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
973 goto err_free_cpumask;
974
975 return policy;
976
977err_free_cpumask:
978 free_cpumask_var(policy->cpus);
979err_free_policy:
980 kfree(policy);
981
982 return NULL;
983}
984
985static void cpufreq_policy_free(struct cpufreq_policy *policy)
986{
987 free_cpumask_var(policy->related_cpus);
988 free_cpumask_var(policy->cpus);
989 kfree(policy);
990}
991
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +0530992static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif,
993 bool frozen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Viresh Kumarfcf80582013-01-29 14:39:08 +0000995 unsigned int j, cpu = dev->id;
Viresh Kumar65922462013-02-07 10:56:03 +0530996 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 unsigned long flags;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500999#ifdef CONFIG_HOTPLUG_CPU
Viresh Kumarfcf80582013-01-29 14:39:08 +00001000 struct cpufreq_governor *gov;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001001 int sibling;
1002#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Ashok Rajc32b6b82005-10-30 14:59:54 -08001004 if (cpu_is_offline(cpu))
1005 return 0;
1006
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001007 pr_debug("adding CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009#ifdef CONFIG_SMP
1010 /* check whether a different CPU already registered this
1011 * CPU because it is in the same boat. */
1012 policy = cpufreq_cpu_get(cpu);
1013 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -05001014 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return 0;
1016 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001017
1018#ifdef CONFIG_HOTPLUG_CPU
1019 /* Check if this cpu was hot-unplugged earlier and has siblings */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001020 read_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +00001021 for_each_online_cpu(sibling) {
1022 struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301023 if (cp && cpumask_test_cpu(cpu, cp->related_cpus)) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001024 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301025 return cpufreq_add_policy_cpu(cpu, sibling, dev,
1026 frozen);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301027 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001028 }
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001029 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +00001030#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031#endif
1032
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001033 if (!try_module_get(cpufreq_driver->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 ret = -EINVAL;
1035 goto module_out;
1036 }
1037
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301038 if (frozen)
1039 /* Restore the saved policy when doing light-weight init */
1040 policy = cpufreq_policy_restore(cpu);
1041 else
1042 policy = cpufreq_policy_alloc();
1043
Dave Jones059019a2009-07-08 16:30:03 -04001044 if (!policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 goto nomem_out;
Dave Jones059019a2009-07-08 16:30:03 -04001046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 policy->cpu = cpu;
Viresh Kumar65922462013-02-07 10:56:03 +05301048 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Rusty Russell835481d2009-01-04 05:18:06 -08001049 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001051 /* Initially set CPU itself as the policy_cpu */
Tejun Heof1625062009-10-29 22:34:13 +09001052 per_cpu(cpufreq_policy_cpu, cpu) = cpu;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 init_completion(&policy->kobj_unregister);
David Howells65f27f32006-11-22 14:55:48 +00001055 INIT_WORK(&policy->update, handle_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 /* call driver. From then on the cpufreq must be able
1058 * to accept all calls to ->verify and ->setpolicy for this CPU
1059 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001060 ret = cpufreq_driver->init(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001062 pr_debug("initialization failed\n");
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301063 goto err_set_policy_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001065
Viresh Kumarfcf80582013-01-29 14:39:08 +00001066 /* related cpus should atleast have policy->cpus */
1067 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
1068
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001069 /*
1070 * affected cpus must always be the one, which are online. We aren't
1071 * managing offline cpus here.
1072 */
1073 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1074
Mike Chan187d9f42008-12-04 12:19:17 -08001075 policy->user_policy.min = policy->min;
1076 policy->user_policy.max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Thomas Renningera1531ac2008-07-29 22:32:58 -07001078 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1079 CPUFREQ_START, policy);
1080
Viresh Kumarfcf80582013-01-29 14:39:08 +00001081#ifdef CONFIG_HOTPLUG_CPU
1082 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
1083 if (gov) {
1084 policy->governor = gov;
1085 pr_debug("Restoring governor %s for cpu %d\n",
1086 policy->governor->name, cpu);
Thomas Renninger4bfa0422009-07-24 15:25:03 +02001087 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001088#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301090 write_lock_irqsave(&cpufreq_driver_lock, flags);
1091 for_each_cpu(j, policy->cpus) {
1092 per_cpu(cpufreq_cpu_data, j) = policy;
1093 per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
1094 }
1095 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1096
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301097 if (!frozen) {
Viresh Kumar308b60e2013-07-31 14:35:14 +02001098 ret = cpufreq_add_dev_interface(policy, dev);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301099 if (ret)
1100 goto err_out_unregister;
1101 }
Dave Jones8ff69732006-03-05 03:37:23 -05001102
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301103 cpufreq_init_policy(policy);
1104
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001105 kobject_uevent(&policy->kobj, KOBJ_ADD);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001106 module_put(cpufreq_driver->owner);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001107 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return 0;
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111err_out_unregister:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001112 write_lock_irqsave(&cpufreq_driver_lock, flags);
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301113 for_each_cpu(j, policy->cpus) {
Mike Travis7a6aedf2008-03-25 15:06:53 -07001114 per_cpu(cpufreq_cpu_data, j) = NULL;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301115 if (j != cpu)
1116 per_cpu(cpufreq_policy_cpu, j) = -1;
1117 }
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001118 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -08001120 kobject_put(&policy->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 wait_for_completion(&policy->kobj_unregister);
1122
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301123err_set_policy_cpu:
1124 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301125 cpufreq_policy_free(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126nomem_out:
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001127 module_put(cpufreq_driver->owner);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001128module_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 return ret;
1130}
1131
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301132/**
1133 * cpufreq_add_dev - add a CPU device
1134 *
1135 * Adds the cpufreq interface for a CPU device.
1136 *
1137 * The Oracle says: try running cpufreq registration/unregistration concurrently
1138 * with with cpu hotplugging and all hell will break loose. Tried to clean this
1139 * mess up, but more thorough testing is needed. - Mathieu
1140 */
1141static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1142{
1143 return __cpufreq_add_dev(dev, sif, false);
1144}
1145
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001146static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
1147{
1148 int j;
1149
1150 policy->last_cpu = policy->cpu;
1151 policy->cpu = cpu;
1152
Viresh Kumar3361b7b2013-02-04 11:38:51 +00001153 for_each_cpu(j, policy->cpus)
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001154 per_cpu(cpufreq_policy_cpu, j) = cpu;
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001155
1156#ifdef CONFIG_CPU_FREQ_TABLE
1157 cpufreq_frequency_table_update_policy_cpu(policy);
1158#endif
1159 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1160 CPUFREQ_UPDATE_POLICY_CPU, policy);
1161}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301163static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy *data,
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301164 unsigned int old_cpu, bool frozen)
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301165{
1166 struct device *cpu_dev;
1167 unsigned long flags;
1168 int ret;
1169
1170 /* first sibling now owns the new sysfs dir */
1171 cpu_dev = get_cpu_device(cpumask_first(data->cpus));
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301172
1173 /* Don't touch sysfs files during light-weight tear-down */
1174 if (frozen)
1175 return cpu_dev->id;
1176
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301177 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
1178 ret = kobject_move(&data->kobj, &cpu_dev->kobj);
1179 if (ret) {
1180 pr_err("%s: Failed to move kobj: %d", __func__, ret);
1181
1182 WARN_ON(lock_policy_rwsem_write(old_cpu));
1183 cpumask_set_cpu(old_cpu, data->cpus);
1184
1185 write_lock_irqsave(&cpufreq_driver_lock, flags);
1186 per_cpu(cpufreq_cpu_data, old_cpu) = data;
1187 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1188
1189 unlock_policy_rwsem_write(old_cpu);
1190
1191 ret = sysfs_create_link(&cpu_dev->kobj, &data->kobj,
1192 "cpufreq");
1193
1194 return -EINVAL;
1195 }
1196
1197 return cpu_dev->id;
1198}
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200/**
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001201 * __cpufreq_remove_dev - remove a CPU device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 *
1203 * Removes the cpufreq interface for a CPU device.
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001204 * Caller should already have policy_rwsem in write mode for this CPU.
1205 * This routine frees the rwsem before returning.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 */
Viresh Kumarbb176f72013-06-19 14:19:33 +05301207static int __cpufreq_remove_dev(struct device *dev,
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301208 struct subsys_interface *sif, bool frozen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301210 unsigned int cpu = dev->id, cpus;
1211 int new_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 unsigned long flags;
1213 struct cpufreq_policy *data;
Amerigo Wang499bca92010-03-04 03:23:46 -05001214 struct kobject *kobj;
1215 struct completion *cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001217 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001219 write_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 data = per_cpu(cpufreq_cpu_data, cpu);
Mike Travis7a6aedf2008-03-25 15:06:53 -07001222 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301224 /* Save the policy somewhere when doing a light-weight tear-down */
1225 if (frozen)
1226 per_cpu(cpufreq_cpu_data_fallback, cpu) = data;
1227
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001228 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 if (!data) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001231 pr_debug("%s: No cpu_data found\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001235 if (cpufreq_driver->target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001237
Jacob Shin27ecddc2011-04-27 13:32:11 -05001238#ifdef CONFIG_HOTPLUG_CPU
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001239 if (!cpufreq_driver->setpolicy)
Dirk Brandewiefa69e332013-02-06 09:02:11 -08001240 strncpy(per_cpu(cpufreq_cpu_governor, cpu),
1241 data->governor->name, CPUFREQ_NAME_LEN);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001242#endif
1243
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301244 WARN_ON(lock_policy_rwsem_write(cpu));
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001245 cpus = cpumask_weight(data->cpus);
Viresh Kumare4969eb2013-04-11 08:04:53 +00001246
1247 if (cpus > 1)
1248 cpumask_clear_cpu(cpu, data->cpus);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301249 unlock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301251 if (cpu != data->cpu && !frozen) {
Viresh Kumar73bf0fc2013-02-05 22:21:14 +01001252 sysfs_remove_link(&dev->kobj, "cpufreq");
1253 } else if (cpus > 1) {
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301254
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301255 new_cpu = cpufreq_nominate_new_policy_cpu(data, cpu, frozen);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301256 if (new_cpu >= 0) {
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301257 WARN_ON(lock_policy_rwsem_write(cpu));
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301258 update_policy_cpu(data, new_cpu);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301259 unlock_policy_rwsem_write(cpu);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301260
1261 if (!frozen) {
1262 pr_debug("%s: policy Kobject moved to cpu: %d "
1263 "from: %d\n",__func__, new_cpu, cpu);
1264 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001265 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001266 }
Venki Pallipadiec282972007-03-26 12:03:19 -07001267
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001268 /* If cpu is last user of policy, free policy */
1269 if (cpus == 1) {
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001270 if (cpufreq_driver->target)
1271 __cpufreq_governor(data, CPUFREQ_GOV_POLICY_EXIT);
1272
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301273 if (!frozen) {
1274 lock_policy_rwsem_read(cpu);
1275 kobj = &data->kobj;
1276 cmp = &data->kobj_unregister;
1277 unlock_policy_rwsem_read(cpu);
1278 kobject_put(kobj);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001279
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301280 /*
1281 * We need to make sure that the underlying kobj is
1282 * actually not referenced anymore by anybody before we
1283 * proceed with unloading.
1284 */
1285 pr_debug("waiting for dropping of refcount\n");
1286 wait_for_completion(cmp);
1287 pr_debug("wait complete\n");
1288 }
1289
1290 /*
1291 * Perform the ->exit() even during light-weight tear-down,
1292 * since this is a core component, and is essential for the
1293 * subsequent light-weight ->init() to succeed.
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001294 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001295 if (cpufreq_driver->exit)
1296 cpufreq_driver->exit(data);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001297
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301298 if (!frozen)
1299 cpufreq_policy_free(data);
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001300 } else {
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301301
1302 if (!frozen) {
1303 pr_debug("%s: removing link, cpu: %d\n", __func__, cpu);
1304 cpufreq_cpu_put(data);
1305 }
1306
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001307 if (cpufreq_driver->target) {
1308 __cpufreq_governor(data, CPUFREQ_GOV_START);
1309 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1310 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301313 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 return 0;
1315}
1316
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001317static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001318{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001319 unsigned int cpu = dev->id;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001320 int retval;
Venki Pallipadiec282972007-03-26 12:03:19 -07001321
1322 if (cpu_is_offline(cpu))
1323 return 0;
1324
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301325 retval = __cpufreq_remove_dev(dev, sif, false);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001326 return retval;
1327}
1328
David Howells65f27f32006-11-22 14:55:48 +00001329static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
David Howells65f27f32006-11-22 14:55:48 +00001331 struct cpufreq_policy *policy =
1332 container_of(work, struct cpufreq_policy, update);
1333 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001334 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 cpufreq_update_policy(cpu);
1336}
1337
1338/**
Viresh Kumarbb176f72013-06-19 14:19:33 +05301339 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1340 * in deep trouble.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 * @cpu: cpu number
1342 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1343 * @new_freq: CPU frequency the CPU actually runs at
1344 *
Dave Jones29464f22009-01-18 01:37:11 -05001345 * We adjust to current frequency first, and need to clean up later.
1346 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301348static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1349 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301351 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301353 unsigned long flags;
1354
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001355 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 freqs.old = old_freq;
1359 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301360
1361 read_lock_irqsave(&cpufreq_driver_lock, flags);
1362 policy = per_cpu(cpufreq_cpu_data, cpu);
1363 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1364
1365 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1366 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367}
1368
Dave Jones32ee8c32006-02-28 00:43:23 -05001369/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301370 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001371 * @cpu: CPU number
1372 *
1373 * This is the last known freq, without actually getting it from the driver.
1374 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1375 */
1376unsigned int cpufreq_quick_get(unsigned int cpu)
1377{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001378 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301379 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001380
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001381 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1382 return cpufreq_driver->get(cpu);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001383
1384 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001385 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301386 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001387 cpufreq_cpu_put(policy);
1388 }
1389
Dave Jones4d34a672008-02-07 16:33:49 -05001390 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001391}
1392EXPORT_SYMBOL(cpufreq_quick_get);
1393
Jesse Barnes3d737102011-06-28 10:59:12 -07001394/**
1395 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1396 * @cpu: CPU number
1397 *
1398 * Just return the max possible frequency for a given CPU.
1399 */
1400unsigned int cpufreq_quick_get_max(unsigned int cpu)
1401{
1402 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1403 unsigned int ret_freq = 0;
1404
1405 if (policy) {
1406 ret_freq = policy->max;
1407 cpufreq_cpu_put(policy);
1408 }
1409
1410 return ret_freq;
1411}
1412EXPORT_SYMBOL(cpufreq_quick_get_max);
1413
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001414static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001416 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301417 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001419 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001420 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001422 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301424 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001425 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301426 /* verify no discrepancy between actual and
1427 saved value exists */
1428 if (unlikely(ret_freq != policy->cur)) {
1429 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 schedule_work(&policy->update);
1431 }
1432 }
1433
Dave Jones4d34a672008-02-07 16:33:49 -05001434 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001435}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001437/**
1438 * cpufreq_get - get the current CPU frequency (in kHz)
1439 * @cpu: CPU number
1440 *
1441 * Get the CPU current (static) CPU frequency
1442 */
1443unsigned int cpufreq_get(unsigned int cpu)
1444{
1445 unsigned int ret_freq = 0;
1446 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1447
1448 if (!policy)
1449 goto out;
1450
1451 if (unlikely(lock_policy_rwsem_read(cpu)))
1452 goto out_policy;
1453
1454 ret_freq = __cpufreq_get(cpu);
1455
1456 unlock_policy_rwsem_read(cpu);
1457
1458out_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 cpufreq_cpu_put(policy);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001460out:
Dave Jones4d34a672008-02-07 16:33:49 -05001461 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462}
1463EXPORT_SYMBOL(cpufreq_get);
1464
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001465static struct subsys_interface cpufreq_interface = {
1466 .name = "cpufreq",
1467 .subsys = &cpu_subsys,
1468 .add_dev = cpufreq_add_dev,
1469 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001470};
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001473 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1474 *
1475 * This function is only executed for the boot processor. The other CPUs
1476 * have been put offline by means of CPU hotplug.
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001477 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001478static int cpufreq_bp_suspend(void)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001479{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301480 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001481
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001482 int cpu = smp_processor_id();
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001483 struct cpufreq_policy *cpu_policy;
1484
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001485 pr_debug("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001486
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001487 /* If there's no policy for the boot CPU, we have nothing to do. */
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001488 cpu_policy = cpufreq_cpu_get(cpu);
1489 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001490 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001491
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001492 if (cpufreq_driver->suspend) {
1493 ret = cpufreq_driver->suspend(cpu_policy);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001494 if (ret)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001495 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1496 "step on CPU %u\n", cpu_policy->cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001497 }
1498
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001499 cpufreq_cpu_put(cpu_policy);
Dave Jonesc9060492008-02-07 16:32:18 -05001500 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001501}
1502
1503/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001504 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 *
1506 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001507 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1508 * restored. It will verify that the current freq is in sync with
1509 * what we believe it to be. This is a bit later than when it
1510 * should be, but nonethteless it's better than calling
1511 * cpufreq_driver->get() here which might re-enable interrupts...
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001512 *
1513 * This function is only executed for the boot CPU. The other CPUs have not
1514 * been turned on yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001516static void cpufreq_bp_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301518 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001519
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001520 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 struct cpufreq_policy *cpu_policy;
1522
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001523 pr_debug("resuming cpu %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001525 /* If there's no policy for the boot CPU, we have nothing to do. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 cpu_policy = cpufreq_cpu_get(cpu);
1527 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001528 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001530 if (cpufreq_driver->resume) {
1531 ret = cpufreq_driver->resume(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 if (ret) {
1533 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1534 "step on CPU %u\n", cpu_policy->cpu);
Dave Jonesc9060492008-02-07 16:32:18 -05001535 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 }
1537 }
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 schedule_work(&cpu_policy->update);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001540
Dave Jonesc9060492008-02-07 16:32:18 -05001541fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543}
1544
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001545static struct syscore_ops cpufreq_syscore_ops = {
1546 .suspend = cpufreq_bp_suspend,
1547 .resume = cpufreq_bp_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548};
1549
Borislav Petkov9d950462013-01-20 10:24:28 +00001550/**
1551 * cpufreq_get_current_driver - return current driver's name
1552 *
1553 * Return the name string of the currently loaded cpufreq driver
1554 * or NULL, if none.
1555 */
1556const char *cpufreq_get_current_driver(void)
1557{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001558 if (cpufreq_driver)
1559 return cpufreq_driver->name;
1560
1561 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001562}
1563EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
1565/*********************************************************************
1566 * NOTIFIER LISTS INTERFACE *
1567 *********************************************************************/
1568
1569/**
1570 * cpufreq_register_notifier - register a driver with cpufreq
1571 * @nb: notifier function to register
1572 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1573 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001574 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 * are notified about clock rate changes (once before and once after
1576 * the transition), or a list of drivers that are notified about
1577 * changes in cpufreq policy.
1578 *
1579 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001580 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 */
1582int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1583{
1584 int ret;
1585
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001586 if (cpufreq_disabled())
1587 return -EINVAL;
1588
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001589 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 switch (list) {
1592 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001593 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001594 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 break;
1596 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001597 ret = blocking_notifier_chain_register(
1598 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 break;
1600 default:
1601 ret = -EINVAL;
1602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604 return ret;
1605}
1606EXPORT_SYMBOL(cpufreq_register_notifier);
1607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608/**
1609 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1610 * @nb: notifier block to be unregistered
Viresh Kumarbb176f72013-06-19 14:19:33 +05301611 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 *
1613 * Remove a driver from the CPU frequency notifier list.
1614 *
1615 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001616 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 */
1618int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1619{
1620 int ret;
1621
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001622 if (cpufreq_disabled())
1623 return -EINVAL;
1624
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 switch (list) {
1626 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001627 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001628 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 break;
1630 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001631 ret = blocking_notifier_chain_unregister(
1632 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 break;
1634 default:
1635 ret = -EINVAL;
1636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
1638 return ret;
1639}
1640EXPORT_SYMBOL(cpufreq_unregister_notifier);
1641
1642
1643/*********************************************************************
1644 * GOVERNORS *
1645 *********************************************************************/
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647int __cpufreq_driver_target(struct cpufreq_policy *policy,
1648 unsigned int target_freq,
1649 unsigned int relation)
1650{
1651 int retval = -EINVAL;
Viresh Kumar72499242012-10-31 01:28:21 +01001652 unsigned int old_target_freq = target_freq;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001653
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001654 if (cpufreq_disabled())
1655 return -ENODEV;
Viresh Kumar7c30ed52013-06-19 10:16:55 +05301656 if (policy->transition_ongoing)
1657 return -EBUSY;
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001658
Viresh Kumar72499242012-10-31 01:28:21 +01001659 /* Make sure that target_freq is within supported range */
1660 if (target_freq > policy->max)
1661 target_freq = policy->max;
1662 if (target_freq < policy->min)
1663 target_freq = policy->min;
1664
1665 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1666 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001667
1668 if (target_freq == policy->cur)
1669 return 0;
1670
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001671 if (cpufreq_driver->target)
1672 retval = cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 return retval;
1675}
1676EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678int cpufreq_driver_target(struct cpufreq_policy *policy,
1679 unsigned int target_freq,
1680 unsigned int relation)
1681{
Julia Lawallf1829e42008-07-25 22:44:53 +02001682 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001684 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
Julia Lawallf1829e42008-07-25 22:44:53 +02001685 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
1687 ret = __cpufreq_driver_target(policy, target_freq, relation);
1688
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001689 unlock_policy_rwsem_write(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Julia Lawallf1829e42008-07-25 22:44:53 +02001691fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 return ret;
1693}
1694EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1695
venkatesh.pallipadi@intel.combf0b90e2008-08-04 11:59:07 -07001696int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu)
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001697{
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001698 if (cpufreq_disabled())
Viresh Kumara262e942013-05-31 06:15:08 +00001699 return 0;
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001700
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001701 if (!cpufreq_driver->getavg)
Viresh Kumar0676f7f2012-10-24 23:39:48 +02001702 return 0;
1703
Viresh Kumara262e942013-05-31 06:15:08 +00001704 return cpufreq_driver->getavg(policy, cpu);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001705}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001706EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001707
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001708/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001709 * when "event" is CPUFREQ_GOV_LIMITS
1710 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301712static int __cpufreq_governor(struct cpufreq_policy *policy,
1713 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
Dave Jonescc993ca2005-07-28 09:43:56 -07001715 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001716
1717 /* Only must be defined when default governor is known to have latency
1718 restrictions, like e.g. conservative or ondemand.
1719 That this is the case is already ensured in Kconfig
1720 */
1721#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1722 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1723#else
1724 struct cpufreq_governor *gov = NULL;
1725#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001726
1727 if (policy->governor->max_transition_latency &&
1728 policy->cpuinfo.transition_latency >
1729 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07001730 if (!gov)
1731 return -EINVAL;
1732 else {
1733 printk(KERN_WARNING "%s governor failed, too long"
1734 " transition latency of HW, fallback"
1735 " to %s governor\n",
1736 policy->governor->name,
1737 gov->name);
1738 policy->governor = gov;
1739 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
1742 if (!try_module_get(policy->governor->owner))
1743 return -EINVAL;
1744
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001745 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301746 policy->cpu, event);
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001747
1748 mutex_lock(&cpufreq_governor_lock);
1749 if ((!policy->governor_enabled && (event == CPUFREQ_GOV_STOP)) ||
1750 (policy->governor_enabled && (event == CPUFREQ_GOV_START))) {
1751 mutex_unlock(&cpufreq_governor_lock);
1752 return -EBUSY;
1753 }
1754
1755 if (event == CPUFREQ_GOV_STOP)
1756 policy->governor_enabled = false;
1757 else if (event == CPUFREQ_GOV_START)
1758 policy->governor_enabled = true;
1759
1760 mutex_unlock(&cpufreq_governor_lock);
1761
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 ret = policy->governor->governor(policy, event);
1763
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001764 if (!ret) {
1765 if (event == CPUFREQ_GOV_POLICY_INIT)
1766 policy->governor->initialized++;
1767 else if (event == CPUFREQ_GOV_POLICY_EXIT)
1768 policy->governor->initialized--;
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001769 } else {
1770 /* Restore original values */
1771 mutex_lock(&cpufreq_governor_lock);
1772 if (event == CPUFREQ_GOV_STOP)
1773 policy->governor_enabled = true;
1774 else if (event == CPUFREQ_GOV_START)
1775 policy->governor_enabled = false;
1776 mutex_unlock(&cpufreq_governor_lock);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001777 }
Viresh Kumarb3940582013-02-01 05:42:58 +00001778
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301779 /* we keep one module reference alive for
1780 each CPU governed by this CPU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 if ((event != CPUFREQ_GOV_START) || ret)
1782 module_put(policy->governor->owner);
1783 if ((event == CPUFREQ_GOV_STOP) && !ret)
1784 module_put(policy->governor->owner);
1785
1786 return ret;
1787}
1788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789int cpufreq_register_governor(struct cpufreq_governor *governor)
1790{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001791 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
1793 if (!governor)
1794 return -EINVAL;
1795
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001796 if (cpufreq_disabled())
1797 return -ENODEV;
1798
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001799 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001800
Viresh Kumarb3940582013-02-01 05:42:58 +00001801 governor->initialized = 0;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001802 err = -EBUSY;
1803 if (__find_governor(governor->name) == NULL) {
1804 err = 0;
1805 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Dave Jones32ee8c32006-02-28 00:43:23 -05001808 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001809 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810}
1811EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1814{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001815#ifdef CONFIG_HOTPLUG_CPU
1816 int cpu;
1817#endif
1818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 if (!governor)
1820 return;
1821
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001822 if (cpufreq_disabled())
1823 return;
1824
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001825#ifdef CONFIG_HOTPLUG_CPU
1826 for_each_present_cpu(cpu) {
1827 if (cpu_online(cpu))
1828 continue;
1829 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1830 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1831 }
1832#endif
1833
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001834 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001836 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 return;
1838}
1839EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1840
1841
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842/*********************************************************************
1843 * POLICY INTERFACE *
1844 *********************************************************************/
1845
1846/**
1847 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05001848 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1849 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 *
1851 * Reads the current cpufreq policy.
1852 */
1853int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1854{
1855 struct cpufreq_policy *cpu_policy;
1856 if (!policy)
1857 return -EINVAL;
1858
1859 cpu_policy = cpufreq_cpu_get(cpu);
1860 if (!cpu_policy)
1861 return -EINVAL;
1862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
1865 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 return 0;
1867}
1868EXPORT_SYMBOL(cpufreq_get_policy);
1869
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001870/*
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301871 * data : current policy.
1872 * policy : policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001873 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301874static int __cpufreq_set_policy(struct cpufreq_policy *data,
1875 struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001877 int ret = 0, failed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001879 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 policy->min, policy->max);
1881
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301882 memcpy(&policy->cpuinfo, &data->cpuinfo,
1883 sizeof(struct cpufreq_cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Yi Yang53391fa2008-01-30 13:33:34 +01001885 if (policy->min > data->max || policy->max < data->min) {
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02001886 ret = -EINVAL;
1887 goto error_out;
1888 }
1889
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 /* verify the cpu speed can be set within this limit */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001891 ret = cpufreq_driver->verify(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 if (ret)
1893 goto error_out;
1894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001896 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1897 CPUFREQ_ADJUST, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
1899 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001900 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1901 CPUFREQ_INCOMPATIBLE, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Viresh Kumarbb176f72013-06-19 14:19:33 +05301903 /*
1904 * verify the cpu speed can be set within this limit, which might be
1905 * different to the first one
1906 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001907 ret = cpufreq_driver->verify(policy);
Alan Sterne041c682006-03-27 01:16:30 -08001908 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
1911 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001912 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1913 CPUFREQ_NOTIFY, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Dave Jones7d5e3502006-02-02 17:03:42 -05001915 data->min = policy->min;
1916 data->max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001918 pr_debug("new min and max freqs are %u - %u kHz\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301919 data->min, data->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001921 if (cpufreq_driver->setpolicy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 data->policy = policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001923 pr_debug("setting range\n");
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001924 ret = cpufreq_driver->setpolicy(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 } else {
1926 if (policy->governor != data->governor) {
1927 /* save old, working values */
1928 struct cpufreq_governor *old_gov = data->governor;
1929
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001930 pr_debug("governor switch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
1932 /* end old governor */
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001933 if (data->governor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Viresh Kumar955ef482013-05-16 05:09:58 +00001935 unlock_policy_rwsem_write(policy->cpu);
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001936 __cpufreq_governor(data,
1937 CPUFREQ_GOV_POLICY_EXIT);
Viresh Kumar955ef482013-05-16 05:09:58 +00001938 lock_policy_rwsem_write(policy->cpu);
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
1941 /* start new governor */
1942 data->governor = policy->governor;
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001943 if (!__cpufreq_governor(data, CPUFREQ_GOV_POLICY_INIT)) {
Viresh Kumar955ef482013-05-16 05:09:58 +00001944 if (!__cpufreq_governor(data, CPUFREQ_GOV_START)) {
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001945 failed = 0;
Viresh Kumar955ef482013-05-16 05:09:58 +00001946 } else {
1947 unlock_policy_rwsem_write(policy->cpu);
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001948 __cpufreq_governor(data,
1949 CPUFREQ_GOV_POLICY_EXIT);
Viresh Kumar955ef482013-05-16 05:09:58 +00001950 lock_policy_rwsem_write(policy->cpu);
1951 }
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001952 }
1953
1954 if (failed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 /* new governor failed, so re-start old one */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001956 pr_debug("starting governor %s failed\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301957 data->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 if (old_gov) {
1959 data->governor = old_gov;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301960 __cpufreq_governor(data,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001961 CPUFREQ_GOV_POLICY_INIT);
1962 __cpufreq_governor(data,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301963 CPUFREQ_GOV_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 }
1965 ret = -EINVAL;
1966 goto error_out;
1967 }
1968 /* might be a policy change, too, so fall through */
1969 }
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001970 pr_debug("governor: change or update limits\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1972 }
1973
Dave Jones7d5e3502006-02-02 17:03:42 -05001974error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 return ret;
1976}
1977
1978/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1980 * @cpu: CPU which shall be re-evaluated
1981 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001982 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 * at different times.
1984 */
1985int cpufreq_update_policy(unsigned int cpu)
1986{
1987 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1988 struct cpufreq_policy policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02001989 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Julia Lawallf1829e42008-07-25 22:44:53 +02001991 if (!data) {
1992 ret = -ENODEV;
1993 goto no_policy;
1994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Julia Lawallf1829e42008-07-25 22:44:53 +02001996 if (unlikely(lock_policy_rwsem_write(cpu))) {
1997 ret = -EINVAL;
1998 goto fail;
1999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002001 pr_debug("updating policy for CPU %u\n", cpu);
Dave Jones7d5e3502006-02-02 17:03:42 -05002002 memcpy(&policy, data, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 policy.min = data->user_policy.min;
2004 policy.max = data->user_policy.max;
2005 policy.policy = data->user_policy.policy;
2006 policy.governor = data->user_policy.governor;
2007
Viresh Kumarbb176f72013-06-19 14:19:33 +05302008 /*
2009 * BIOS might change freq behind our back
2010 * -> ask driver for current freq and notify governors about a change
2011 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002012 if (cpufreq_driver->get) {
2013 policy.cur = cpufreq_driver->get(cpu);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002014 if (!data->cur) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002015 pr_debug("Driver did not initialize current freq");
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002016 data->cur = policy.cur;
2017 } else {
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002018 if (data->cur != policy.cur && cpufreq_driver->target)
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05302019 cpufreq_out_of_sync(cpu, data->cur,
2020 policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002021 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01002022 }
2023
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 ret = __cpufreq_set_policy(data, &policy);
2025
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002026 unlock_policy_rwsem_write(cpu);
2027
Julia Lawallf1829e42008-07-25 22:44:53 +02002028fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 cpufreq_cpu_put(data);
Julia Lawallf1829e42008-07-25 22:44:53 +02002030no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 return ret;
2032}
2033EXPORT_SYMBOL(cpufreq_update_policy);
2034
Paul Gortmaker27609842013-06-19 13:54:04 -04002035static int cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002036 unsigned long action, void *hcpu)
2037{
2038 unsigned int cpu = (unsigned long)hcpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002039 struct device *dev;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302040 bool frozen = false;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002041
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002042 dev = get_cpu_device(cpu);
2043 if (dev) {
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302044
2045 if (action & CPU_TASKS_FROZEN)
2046 frozen = true;
2047
2048 switch (action & ~CPU_TASKS_FROZEN) {
Ashok Rajc32b6b82005-10-30 14:59:54 -08002049 case CPU_ONLINE:
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302050 __cpufreq_add_dev(dev, NULL, frozen);
Srivatsa S. Bhat23d328992013-07-30 04:23:56 +05302051 cpufreq_update_policy(cpu);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002052 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302053
Ashok Rajc32b6b82005-10-30 14:59:54 -08002054 case CPU_DOWN_PREPARE:
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302055 __cpufreq_remove_dev(dev, NULL, frozen);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002056 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302057
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002058 case CPU_DOWN_FAILED:
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302059 __cpufreq_add_dev(dev, NULL, frozen);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002060 break;
2061 }
2062 }
2063 return NOTIFY_OK;
2064}
2065
Neal Buckendahl9c36f742010-06-22 22:02:44 -05002066static struct notifier_block __refdata cpufreq_cpu_notifier = {
Viresh Kumarbb176f72013-06-19 14:19:33 +05302067 .notifier_call = cpufreq_cpu_callback,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002068};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070/*********************************************************************
2071 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2072 *********************************************************************/
2073
2074/**
2075 * cpufreq_register_driver - register a CPU Frequency driver
2076 * @driver_data: A struct cpufreq_driver containing the values#
2077 * submitted by the CPU Frequency driver.
2078 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302079 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05002081 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 *
2083 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002084int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085{
2086 unsigned long flags;
2087 int ret;
2088
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002089 if (cpufreq_disabled())
2090 return -ENODEV;
2091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 if (!driver_data || !driver_data->verify || !driver_data->init ||
2093 ((!driver_data->setpolicy) && (!driver_data->target)))
2094 return -EINVAL;
2095
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002096 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
2098 if (driver_data->setpolicy)
2099 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2100
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002101 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002102 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002103 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 return -EBUSY;
2105 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002106 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002107 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002109 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002110 if (ret)
2111 goto err_null_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002113 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 int i;
2115 ret = -ENODEV;
2116
2117 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07002118 for (i = 0; i < nr_cpu_ids; i++)
2119 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07002121 break;
2122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
2124 /* if all ->init() calls failed, unregister */
2125 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002126 pr_debug("no CPU initialized for driver %s\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05302127 driver_data->name);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002128 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 }
2130 }
2131
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002132 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002133 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002135 return 0;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002136err_if_unreg:
2137 subsys_interface_unregister(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002138err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002139 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002140 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002141 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05002142 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143}
2144EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146/**
2147 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2148 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302149 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 * the right to do so, i.e. if you have succeeded in initialising before!
2151 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2152 * currently not initialised.
2153 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002154int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
2156 unsigned long flags;
2157
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002158 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002161 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002163 subsys_interface_unregister(&cpufreq_interface);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07002164 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002166 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002167 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002168 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
2170 return 0;
2171}
2172EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002173
2174static int __init cpufreq_core_init(void)
2175{
2176 int cpu;
2177
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002178 if (cpufreq_disabled())
2179 return -ENODEV;
2180
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002181 for_each_possible_cpu(cpu) {
Tejun Heof1625062009-10-29 22:34:13 +09002182 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002183 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
2184 }
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002185
Viresh Kumar2361be22013-05-17 16:09:09 +05302186 cpufreq_global_kobject = kobject_create();
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002187 BUG_ON(!cpufreq_global_kobject);
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01002188 register_syscore_ops(&cpufreq_syscore_ops);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002189
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002190 return 0;
2191}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002192core_initcall(cpufreq_core_init);