blob: 2f5801adc7391f507e3f1f84dac07b2bd32af660 [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>
6 *
Ashok Rajc32b6b82005-10-30 14:59:54 -08007 * Oct 2005 - Ashok Raj <ashok.raj@intel.com>
Dave Jones32ee8c32006-02-28 00:43:23 -05008 * Added handling for CPU hotplug
Dave Jones8ff69732006-03-05 03:37:23 -05009 * Feb 2006 - Jacob Shin <jacob.shin@amd.com>
10 * Fix handling for CPU hotplug -- affected CPUs
Ashok Rajc32b6b82005-10-30 14:59:54 -080011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 *
16 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/init.h>
21#include <linux/notifier.h>
22#include <linux/cpufreq.h>
23#include <linux/delay.h>
24#include <linux/interrupt.h>
25#include <linux/spinlock.h>
26#include <linux/device.h>
27#include <linux/slab.h>
28#include <linux/cpu.h>
29#include <linux/completion.h>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080030#include <linux/mutex.h>
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +010031#include <linux/syscore_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Thomas Renninger6f4f2722010-04-20 13:17:36 +020033#include <trace/events/power.h>
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035/**
Dave Jonescd878472006-08-11 17:59:28 -040036 * The "cpufreq driver" - the arch- or hardware-dependent low
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * level driver of CPUFreq support, and its spinlock. This lock
38 * also protects the cpufreq_cpu_data array.
39 */
Dave Jones7d5e3502006-02-02 17:03:42 -050040static struct cpufreq_driver *cpufreq_driver;
Mike Travis7a6aedf2008-03-25 15:06:53 -070041static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
Thomas Renninger084f3492007-07-09 11:35:28 -070042#ifdef CONFIG_HOTPLUG_CPU
43/* This one keeps track of the previously set governor of a removed CPU */
Dmitry Monakhove77b89f2009-10-05 00:38:55 +040044static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
Thomas Renninger084f3492007-07-09 11:35:28 -070045#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static DEFINE_SPINLOCK(cpufreq_driver_lock);
47
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080048/*
49 * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
50 * all cpufreq/hotplug/workqueue/etc related lock issues.
51 *
52 * The rules for this semaphore:
53 * - Any routine that wants to read from the policy structure will
54 * do a down_read on this semaphore.
55 * - Any routine that will write to the policy structure and/or may take away
56 * the policy altogether (eg. CPU hotplug), will hold this lock in write
57 * mode before doing so.
58 *
59 * Additional rules:
60 * - All holders of the lock should check to make sure that the CPU they
61 * are concerned with are online after they get the lock.
62 * - Governor routines that can be called in cpufreq hotplug path should not
63 * take this sem as top level hotplug notifier handler takes this.
Mathieu Desnoyers395913d2009-06-08 13:17:31 -040064 * - Lock should not be held across
65 * __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080066 */
Tejun Heof1625062009-10-29 22:34:13 +090067static DEFINE_PER_CPU(int, cpufreq_policy_cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080068static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
69
70#define lock_policy_rwsem(mode, cpu) \
Amerigo Wang226528c2010-03-04 03:23:36 -050071static int lock_policy_rwsem_##mode \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080072(int cpu) \
73{ \
Tejun Heof1625062009-10-29 22:34:13 +090074 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080075 BUG_ON(policy_cpu == -1); \
76 down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
77 if (unlikely(!cpu_online(cpu))) { \
78 up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
79 return -1; \
80 } \
81 \
82 return 0; \
83}
84
85lock_policy_rwsem(read, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080086
87lock_policy_rwsem(write, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080088
Amerigo Wang226528c2010-03-04 03:23:36 -050089static void unlock_policy_rwsem_read(int cpu)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080090{
Tejun Heof1625062009-10-29 22:34:13 +090091 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080092 BUG_ON(policy_cpu == -1);
93 up_read(&per_cpu(cpu_policy_rwsem, policy_cpu));
94}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080095
Amerigo Wang226528c2010-03-04 03:23:36 -050096static void unlock_policy_rwsem_write(int cpu)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080097{
Tejun Heof1625062009-10-29 22:34:13 +090098 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080099 BUG_ON(policy_cpu == -1);
100 up_write(&per_cpu(cpu_policy_rwsem, policy_cpu));
101}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800102
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/* internal prototypes */
Dave Jones29464f22009-01-18 01:37:11 -0500105static int __cpufreq_governor(struct cpufreq_policy *policy,
106 unsigned int event);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800107static unsigned int __cpufreq_get(unsigned int cpu);
David Howells65f27f32006-11-22 14:55:48 +0000108static void handle_update(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110/**
Dave Jones32ee8c32006-02-28 00:43:23 -0500111 * Two notifier lists: the "policy" list is involved in the
112 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * "transition" list for kernel code that needs to handle
114 * changes to devices when the CPU clock speed changes.
115 * The mutex locks both lists.
116 */
Alan Sterne041c682006-03-27 01:16:30 -0800117static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700118static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200120static bool init_cpufreq_transition_notifier_list_called;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700121static int __init init_cpufreq_transition_notifier_list(void)
122{
123 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200124 init_cpufreq_transition_notifier_list_called = true;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700125 return 0;
126}
Linus Torvaldsb3438f82006-11-20 11:47:18 -0800127pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129static LIST_HEAD(cpufreq_governor_list);
Dave Jones29464f22009-01-18 01:37:11 -0500130static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Dave Jones7d5e3502006-02-02 17:03:42 -0500132struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 struct cpufreq_policy *data;
135 unsigned long flags;
136
Mike Travis7a6aedf2008-03-25 15:06:53 -0700137 if (cpu >= nr_cpu_ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 goto err_out;
139
140 /* get the cpufreq driver */
141 spin_lock_irqsave(&cpufreq_driver_lock, flags);
142
143 if (!cpufreq_driver)
144 goto err_out_unlock;
145
146 if (!try_module_get(cpufreq_driver->owner))
147 goto err_out_unlock;
148
149
150 /* get the CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -0700151 data = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 if (!data)
154 goto err_out_put_module;
155
156 if (!kobject_get(&data->kobj))
157 goto err_out_put_module;
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return data;
161
Dave Jones7d5e3502006-02-02 17:03:42 -0500162err_out_put_module:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 module_put(cpufreq_driver->owner);
Dave Jones7d5e3502006-02-02 17:03:42 -0500164err_out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones7d5e3502006-02-02 17:03:42 -0500166err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return NULL;
168}
169EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
170
Dave Jones7d5e3502006-02-02 17:03:42 -0500171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172void cpufreq_cpu_put(struct cpufreq_policy *data)
173{
174 kobject_put(&data->kobj);
175 module_put(cpufreq_driver->owner);
176}
177EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
178
179
180/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
182 *********************************************************************/
183
184/**
185 * adjust_jiffies - adjust the system "loops_per_jiffy"
186 *
187 * This function alters the system "loops_per_jiffy" for the clock
188 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500189 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 * per-CPU loops_per_jiffy value wherever possible.
191 */
192#ifndef CONFIG_SMP
193static unsigned long l_p_j_ref;
194static unsigned int l_p_j_ref_freq;
195
Arjan van de Ven858119e2006-01-14 13:20:43 -0800196static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 if (ci->flags & CPUFREQ_CONST_LOOPS)
199 return;
200
201 if (!l_p_j_ref_freq) {
202 l_p_j_ref = loops_per_jiffy;
203 l_p_j_ref_freq = ci->old;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200204 pr_debug("saving %lu as reference value for loops_per_jiffy; "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530205 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
Afzal Mohammedd08de0c12012-01-04 10:52:46 +0530207 if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700208 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530209 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
210 ci->new);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200211 pr_debug("scaling loops_per_jiffy to %lu "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530212 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
214}
215#else
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530216static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
217{
218 return;
219}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220#endif
221
222
223/**
Dave Jonese4472cb2006-01-31 15:53:55 -0800224 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
225 * on frequency transition.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 *
Dave Jonese4472cb2006-01-31 15:53:55 -0800227 * This function calls the transition notifiers and the "adjust_jiffies"
228 * function. It is called twice on all CPU frequency changes that have
Dave Jones32ee8c32006-02-28 00:43:23 -0500229 * external effects.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 */
231void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
232{
Dave Jonese4472cb2006-01-31 15:53:55 -0800233 struct cpufreq_policy *policy;
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 BUG_ON(irqs_disabled());
236
237 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200238 pr_debug("notification %u of frequency transition to %u kHz\n",
Dave Jonese4472cb2006-01-31 15:53:55 -0800239 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Mike Travis7a6aedf2008-03-25 15:06:53 -0700241 policy = per_cpu(cpufreq_cpu_data, freqs->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500245 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800246 * which is not equal to what the cpufreq core thinks is
247 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 */
249 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800250 if ((policy) && (policy->cpu == freqs->cpu) &&
251 (policy->cur) && (policy->cur != freqs->old)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200252 pr_debug("Warning: CPU frequency is"
Dave Jonese4472cb2006-01-31 15:53:55 -0800253 " %u, cpufreq assumed %u kHz.\n",
254 freqs->old, policy->cur);
255 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700258 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800259 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
261 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 case CPUFREQ_POSTCHANGE:
264 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200265 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
Thomas Renninger6f4f2722010-04-20 13:17:36 +0200266 (unsigned long)freqs->cpu);
267 trace_power_frequency(POWER_PSTATE, freqs->new, freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100268 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700269 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800270 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800271 if (likely(policy) && likely(policy->cpu == freqs->cpu))
272 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 break;
274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
277
278
279
280/*********************************************************************
281 * SYSFS INTERFACE *
282 *********************************************************************/
283
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700284static struct cpufreq_governor *__find_governor(const char *str_governor)
285{
286 struct cpufreq_governor *t;
287
288 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
Dave Jones29464f22009-01-18 01:37:11 -0500289 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700290 return t;
291
292 return NULL;
293}
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295/**
296 * cpufreq_parse_governor - parse a governor string
297 */
Dave Jones905d77c2008-03-05 14:28:32 -0500298static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 struct cpufreq_governor **governor)
300{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700301 int err = -EINVAL;
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700304 goto out;
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (cpufreq_driver->setpolicy) {
307 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
308 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700309 err = 0;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530310 } else if (!strnicmp(str_governor, "powersave",
311 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700313 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700315 } else if (cpufreq_driver->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700317
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800318 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700319
320 t = __find_governor(str_governor);
321
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700322 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700323 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700324
Kees Cook1a8e1462011-05-04 08:38:56 -0700325 mutex_unlock(&cpufreq_governor_mutex);
326 ret = request_module("cpufreq_%s", str_governor);
327 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700328
Kees Cook1a8e1462011-05-04 08:38:56 -0700329 if (ret == 0)
330 t = __find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700331 }
332
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700333 if (t != NULL) {
334 *governor = t;
335 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700337
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800338 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
Dave Jones29464f22009-01-18 01:37:11 -0500340out:
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700341 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530346 * cpufreq_per_cpu_attr_read() / show_##file_name() -
347 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 *
349 * Write out information from cpufreq_driver->policy[cpu]; object must be
350 * "unsigned int".
351 */
352
Dave Jones32ee8c32006-02-28 00:43:23 -0500353#define show_one(file_name, object) \
354static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500355(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500356{ \
Dave Jones29464f22009-01-18 01:37:11 -0500357 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
360show_one(cpuinfo_min_freq, cpuinfo.min_freq);
361show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100362show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363show_one(scaling_min_freq, min);
364show_one(scaling_max_freq, max);
365show_one(scaling_cur_freq, cur);
366
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530367static int __cpufreq_set_policy(struct cpufreq_policy *data,
368 struct cpufreq_policy *policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370/**
371 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
372 */
373#define store_one(file_name, object) \
374static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500375(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{ \
377 unsigned int ret = -EINVAL; \
378 struct cpufreq_policy new_policy; \
379 \
380 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
381 if (ret) \
382 return -EINVAL; \
383 \
Dave Jones29464f22009-01-18 01:37:11 -0500384 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (ret != 1) \
386 return -EINVAL; \
387 \
Thomas Renninger7970e082006-04-13 15:14:04 +0200388 ret = __cpufreq_set_policy(policy, &new_policy); \
389 policy->user_policy.object = policy->object; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 \
391 return ret ? ret : count; \
392}
393
Dave Jones29464f22009-01-18 01:37:11 -0500394store_one(scaling_min_freq, min);
395store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397/**
398 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
399 */
Dave Jones905d77c2008-03-05 14:28:32 -0500400static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
401 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800403 unsigned int cur_freq = __cpufreq_get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if (!cur_freq)
405 return sprintf(buf, "<unknown>");
406 return sprintf(buf, "%u\n", cur_freq);
407}
408
409
410/**
411 * show_scaling_governor - show the current policy for the specified CPU
412 */
Dave Jones905d77c2008-03-05 14:28:32 -0500413static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Dave Jones29464f22009-01-18 01:37:11 -0500415 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return sprintf(buf, "powersave\n");
417 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
418 return sprintf(buf, "performance\n");
419 else if (policy->governor)
Dave Jones29464f22009-01-18 01:37:11 -0500420 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n",
421 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return -EINVAL;
423}
424
425
426/**
427 * store_scaling_governor - store policy for the specified CPU
428 */
Dave Jones905d77c2008-03-05 14:28:32 -0500429static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
430 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 unsigned int ret = -EINVAL;
433 char str_governor[16];
434 struct cpufreq_policy new_policy;
435
436 ret = cpufreq_get_policy(&new_policy, policy->cpu);
437 if (ret)
438 return ret;
439
Dave Jones29464f22009-01-18 01:37:11 -0500440 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (ret != 1)
442 return -EINVAL;
443
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530444 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
445 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return -EINVAL;
447
Thomas Renninger7970e082006-04-13 15:14:04 +0200448 /* Do not use cpufreq_set_policy here or the user_policy.max
449 will be wrongly overridden */
Thomas Renninger7970e082006-04-13 15:14:04 +0200450 ret = __cpufreq_set_policy(policy, &new_policy);
451
452 policy->user_policy.policy = policy->policy;
453 policy->user_policy.governor = policy->governor;
Thomas Renninger7970e082006-04-13 15:14:04 +0200454
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530455 if (ret)
456 return ret;
457 else
458 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
461/**
462 * show_scaling_driver - show the cpufreq driver currently loaded
463 */
Dave Jones905d77c2008-03-05 14:28:32 -0500464static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
467}
468
469/**
470 * show_scaling_available_governors - show the available CPUfreq governors
471 */
Dave Jones905d77c2008-03-05 14:28:32 -0500472static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
473 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 ssize_t i = 0;
476 struct cpufreq_governor *t;
477
478 if (!cpufreq_driver->target) {
479 i += sprintf(buf, "performance powersave");
480 goto out;
481 }
482
483 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
Dave Jones29464f22009-01-18 01:37:11 -0500484 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
485 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 goto out;
487 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
488 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500489out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 i += sprintf(&buf[i], "\n");
491 return i;
492}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700493
Rusty Russell835481d2009-01-04 05:18:06 -0800494static ssize_t show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 ssize_t i = 0;
497 unsigned int cpu;
498
Rusty Russell835481d2009-01-04 05:18:06 -0800499 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (i)
501 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
502 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
503 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500504 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
506 i += sprintf(&buf[i], "\n");
507 return i;
508}
509
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700510/**
511 * show_related_cpus - show the CPUs affected by each transition even if
512 * hw coordination is in use
513 */
514static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
515{
Rusty Russell835481d2009-01-04 05:18:06 -0800516 if (cpumask_empty(policy->related_cpus))
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700517 return show_cpus(policy->cpus, buf);
518 return show_cpus(policy->related_cpus, buf);
519}
520
521/**
522 * show_affected_cpus - show the CPUs affected by each transition
523 */
524static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
525{
526 return show_cpus(policy->cpus, buf);
527}
528
Venki Pallipadi9e769882007-10-26 10:18:21 -0700529static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500530 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700531{
532 unsigned int freq = 0;
533 unsigned int ret;
534
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700535 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700536 return -EINVAL;
537
538 ret = sscanf(buf, "%u", &freq);
539 if (ret != 1)
540 return -EINVAL;
541
542 policy->governor->store_setspeed(policy, freq);
543
544 return count;
545}
546
547static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
548{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700549 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700550 return sprintf(buf, "<unsupported>\n");
551
552 return policy->governor->show_setspeed(policy, buf);
553}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Thomas Renningere2f74f32009-11-19 12:31:01 +0100555/**
556 * show_scaling_driver - show the current cpufreq HW/BIOS limitation
557 */
558static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
559{
560 unsigned int limit;
561 int ret;
562 if (cpufreq_driver->bios_limit) {
563 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
564 if (!ret)
565 return sprintf(buf, "%u\n", limit);
566 }
567 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
568}
569
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200570cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
571cpufreq_freq_attr_ro(cpuinfo_min_freq);
572cpufreq_freq_attr_ro(cpuinfo_max_freq);
573cpufreq_freq_attr_ro(cpuinfo_transition_latency);
574cpufreq_freq_attr_ro(scaling_available_governors);
575cpufreq_freq_attr_ro(scaling_driver);
576cpufreq_freq_attr_ro(scaling_cur_freq);
577cpufreq_freq_attr_ro(bios_limit);
578cpufreq_freq_attr_ro(related_cpus);
579cpufreq_freq_attr_ro(affected_cpus);
580cpufreq_freq_attr_rw(scaling_min_freq);
581cpufreq_freq_attr_rw(scaling_max_freq);
582cpufreq_freq_attr_rw(scaling_governor);
583cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Dave Jones905d77c2008-03-05 14:28:32 -0500585static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 &cpuinfo_min_freq.attr,
587 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100588 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 &scaling_min_freq.attr,
590 &scaling_max_freq.attr,
591 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700592 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 &scaling_governor.attr,
594 &scaling_driver.attr,
595 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700596 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 NULL
598};
599
Thomas Renninger8aa84ad2009-07-24 15:25:05 +0200600struct kobject *cpufreq_global_kobject;
601EXPORT_SYMBOL(cpufreq_global_kobject);
602
Dave Jones29464f22009-01-18 01:37:11 -0500603#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
604#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Dave Jones29464f22009-01-18 01:37:11 -0500606static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Dave Jones905d77c2008-03-05 14:28:32 -0500608 struct cpufreq_policy *policy = to_policy(kobj);
609 struct freq_attr *fattr = to_attr(attr);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500610 ssize_t ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 policy = cpufreq_cpu_get(policy->cpu);
612 if (!policy)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500613 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800614
615 if (lock_policy_rwsem_read(policy->cpu) < 0)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500616 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800617
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530618 if (fattr->show)
619 ret = fattr->show(policy, buf);
620 else
621 ret = -EIO;
622
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800623 unlock_policy_rwsem_read(policy->cpu);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500624fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 cpufreq_cpu_put(policy);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500626no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return ret;
628}
629
Dave Jones905d77c2008-03-05 14:28:32 -0500630static ssize_t store(struct kobject *kobj, struct attribute *attr,
631 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Dave Jones905d77c2008-03-05 14:28:32 -0500633 struct cpufreq_policy *policy = to_policy(kobj);
634 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500635 ssize_t ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 policy = cpufreq_cpu_get(policy->cpu);
637 if (!policy)
Dave Jonesa07530b2008-03-05 14:22:25 -0500638 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800639
640 if (lock_policy_rwsem_write(policy->cpu) < 0)
Dave Jonesa07530b2008-03-05 14:22:25 -0500641 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800642
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530643 if (fattr->store)
644 ret = fattr->store(policy, buf, count);
645 else
646 ret = -EIO;
647
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800648 unlock_policy_rwsem_write(policy->cpu);
Dave Jonesa07530b2008-03-05 14:22:25 -0500649fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 cpufreq_cpu_put(policy);
Dave Jonesa07530b2008-03-05 14:22:25 -0500651no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return ret;
653}
654
Dave Jones905d77c2008-03-05 14:28:32 -0500655static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Dave Jones905d77c2008-03-05 14:28:32 -0500657 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200658 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 complete(&policy->kobj_unregister);
660}
661
Emese Revfy52cf25d2010-01-19 02:58:23 +0100662static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 .show = show,
664 .store = store,
665};
666
667static struct kobj_type ktype_cpufreq = {
668 .sysfs_ops = &sysfs_ops,
669 .default_attrs = default_attrs,
670 .release = cpufreq_sysfs_release,
671};
672
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200673/*
674 * Returns:
675 * Negative: Failure
676 * 0: Success
677 * Positive: When we have a managed CPU and the sysfs got symlinked
678 */
Alex Chiangcf3289d02009-11-17 20:27:08 -0700679static int cpufreq_add_dev_policy(unsigned int cpu,
680 struct cpufreq_policy *policy,
681 struct sys_device *sys_dev)
Dave Jonesecf7e462009-07-08 18:48:47 -0400682{
683 int ret = 0;
684#ifdef CONFIG_SMP
685 unsigned long flags;
686 unsigned int j;
Dave Jonesecf7e462009-07-08 18:48:47 -0400687#ifdef CONFIG_HOTPLUG_CPU
Dmitry Monakhove77b89f2009-10-05 00:38:55 +0400688 struct cpufreq_governor *gov;
689
690 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
691 if (gov) {
692 policy->governor = gov;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200693 pr_debug("Restoring governor %s for cpu %d\n",
Dave Jonesecf7e462009-07-08 18:48:47 -0400694 policy->governor->name, cpu);
695 }
696#endif
697
698 for_each_cpu(j, policy->cpus) {
699 struct cpufreq_policy *managed_policy;
700
701 if (cpu == j)
702 continue;
703
704 /* Check for existing affected CPUs.
705 * They may not be aware of it due to CPU Hotplug.
706 * cpufreq_cpu_put is called when the device is removed
707 * in __cpufreq_remove_dev()
708 */
709 managed_policy = cpufreq_cpu_get(j);
710 if (unlikely(managed_policy)) {
711
712 /* Set proper policy_cpu */
713 unlock_policy_rwsem_write(cpu);
Tejun Heof1625062009-10-29 22:34:13 +0900714 per_cpu(cpufreq_policy_cpu, cpu) = managed_policy->cpu;
Dave Jonesecf7e462009-07-08 18:48:47 -0400715
716 if (lock_policy_rwsem_write(cpu) < 0) {
717 /* Should not go through policy unlock path */
718 if (cpufreq_driver->exit)
719 cpufreq_driver->exit(policy);
720 cpufreq_cpu_put(managed_policy);
721 return -EBUSY;
722 }
723
724 spin_lock_irqsave(&cpufreq_driver_lock, flags);
725 cpumask_copy(managed_policy->cpus, policy->cpus);
726 per_cpu(cpufreq_cpu_data, cpu) = managed_policy;
727 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
728
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200729 pr_debug("CPU already managed, adding link\n");
Dave Jonesecf7e462009-07-08 18:48:47 -0400730 ret = sysfs_create_link(&sys_dev->kobj,
731 &managed_policy->kobj,
732 "cpufreq");
733 if (ret)
734 cpufreq_cpu_put(managed_policy);
735 /*
736 * Success. We only needed to be added to the mask.
737 * Call driver->exit() because only the cpu parent of
738 * the kobj needed to call init().
739 */
740 if (cpufreq_driver->exit)
741 cpufreq_driver->exit(policy);
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200742
743 if (!ret)
744 return 1;
745 else
746 return ret;
Dave Jonesecf7e462009-07-08 18:48:47 -0400747 }
748 }
749#endif
750 return ret;
751}
752
753
Dave Jones19d6f7e2009-07-08 17:35:39 -0400754/* symlink affected CPUs */
Alex Chiangcf3289d02009-11-17 20:27:08 -0700755static int cpufreq_add_dev_symlink(unsigned int cpu,
756 struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400757{
758 unsigned int j;
759 int ret = 0;
760
761 for_each_cpu(j, policy->cpus) {
762 struct cpufreq_policy *managed_policy;
763 struct sys_device *cpu_sys_dev;
764
765 if (j == cpu)
766 continue;
767 if (!cpu_online(j))
768 continue;
769
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200770 pr_debug("CPU %u already managed, adding link\n", j);
Dave Jones19d6f7e2009-07-08 17:35:39 -0400771 managed_policy = cpufreq_cpu_get(cpu);
772 cpu_sys_dev = get_cpu_sysdev(j);
773 ret = sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
774 "cpufreq");
775 if (ret) {
776 cpufreq_cpu_put(managed_policy);
777 return ret;
778 }
779 }
780 return ret;
781}
782
Alex Chiangcf3289d02009-11-17 20:27:08 -0700783static int cpufreq_add_dev_interface(unsigned int cpu,
784 struct cpufreq_policy *policy,
785 struct sys_device *sys_dev)
Dave Jones909a6942009-07-08 18:05:42 -0400786{
Dave Jonesecf7e462009-07-08 18:48:47 -0400787 struct cpufreq_policy new_policy;
Dave Jones909a6942009-07-08 18:05:42 -0400788 struct freq_attr **drv_attr;
789 unsigned long flags;
790 int ret = 0;
791 unsigned int j;
792
793 /* prepare interface data */
794 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
795 &sys_dev->kobj, "cpufreq");
796 if (ret)
797 return ret;
798
799 /* set up files for this cpu device */
800 drv_attr = cpufreq_driver->attr;
801 while ((drv_attr) && (*drv_attr)) {
802 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
803 if (ret)
804 goto err_out_kobj_put;
805 drv_attr++;
806 }
807 if (cpufreq_driver->get) {
808 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
809 if (ret)
810 goto err_out_kobj_put;
811 }
812 if (cpufreq_driver->target) {
813 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
814 if (ret)
815 goto err_out_kobj_put;
816 }
Thomas Renningere2f74f32009-11-19 12:31:01 +0100817 if (cpufreq_driver->bios_limit) {
818 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
819 if (ret)
820 goto err_out_kobj_put;
821 }
Dave Jones909a6942009-07-08 18:05:42 -0400822
823 spin_lock_irqsave(&cpufreq_driver_lock, flags);
824 for_each_cpu(j, policy->cpus) {
Julia Lawallbec037a2010-08-05 22:23:00 +0200825 if (!cpu_online(j))
826 continue;
Dave Jones909a6942009-07-08 18:05:42 -0400827 per_cpu(cpufreq_cpu_data, j) = policy;
Tejun Heof1625062009-10-29 22:34:13 +0900828 per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
Dave Jones909a6942009-07-08 18:05:42 -0400829 }
830 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
831
832 ret = cpufreq_add_dev_symlink(cpu, policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400833 if (ret)
834 goto err_out_kobj_put;
835
836 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
837 /* assure that the starting sequence is run in __cpufreq_set_policy */
838 policy->governor = NULL;
839
840 /* set default policy */
841 ret = __cpufreq_set_policy(policy, &new_policy);
842 policy->user_policy.policy = policy->policy;
843 policy->user_policy.governor = policy->governor;
844
845 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200846 pr_debug("setting policy failed\n");
Dave Jonesecf7e462009-07-08 18:48:47 -0400847 if (cpufreq_driver->exit)
848 cpufreq_driver->exit(policy);
849 }
Dave Jones909a6942009-07-08 18:05:42 -0400850 return ret;
851
852err_out_kobj_put:
853 kobject_put(&policy->kobj);
854 wait_for_completion(&policy->kobj_unregister);
855 return ret;
856}
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859/**
860 * cpufreq_add_dev - add a CPU device
861 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500862 * Adds the cpufreq interface for a CPU device.
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400863 *
864 * The Oracle says: try running cpufreq registration/unregistration concurrently
865 * with with cpu hotplugging and all hell will break loose. Tried to clean this
866 * mess up, but more thorough testing is needed. - Mathieu
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 */
Dave Jones905d77c2008-03-05 14:28:32 -0500868static int cpufreq_add_dev(struct sys_device *sys_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
870 unsigned int cpu = sys_dev->id;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500871 int ret = 0, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 unsigned long flags;
874 unsigned int j;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500875#ifdef CONFIG_HOTPLUG_CPU
876 int sibling;
877#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Ashok Rajc32b6b82005-10-30 14:59:54 -0800879 if (cpu_is_offline(cpu))
880 return 0;
881
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200882 pr_debug("adding CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884#ifdef CONFIG_SMP
885 /* check whether a different CPU already registered this
886 * CPU because it is in the same boat. */
887 policy = cpufreq_cpu_get(cpu);
888 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -0500889 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return 0;
891 }
892#endif
893
894 if (!try_module_get(cpufreq_driver->owner)) {
895 ret = -EINVAL;
896 goto module_out;
897 }
898
Dave Jones059019a2009-07-08 16:30:03 -0400899 ret = -ENOMEM;
Dave Jonese98df502005-10-20 15:17:43 -0700900 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
Dave Jones059019a2009-07-08 16:30:03 -0400901 if (!policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 goto nomem_out;
Dave Jones059019a2009-07-08 16:30:03 -0400903
904 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400905 goto err_free_policy;
Dave Jones059019a2009-07-08 16:30:03 -0400906
907 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400908 goto err_free_cpumask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 policy->cpu = cpu;
Rusty Russell835481d2009-01-04 05:18:06 -0800911 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800913 /* Initially set CPU itself as the policy_cpu */
Tejun Heof1625062009-10-29 22:34:13 +0900914 per_cpu(cpufreq_policy_cpu, cpu) = cpu;
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400915 ret = (lock_policy_rwsem_write(cpu) < 0);
916 WARN_ON(ret);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 init_completion(&policy->kobj_unregister);
David Howells65f27f32006-11-22 14:55:48 +0000919 INIT_WORK(&policy->update, handle_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Thomas Renninger8122c6c2007-10-02 13:28:09 -0700921 /* Set governor before ->init, so that driver could check it */
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500922#ifdef CONFIG_HOTPLUG_CPU
923 for_each_online_cpu(sibling) {
924 struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
925 if (cp && cp->governor &&
926 (cpumask_test_cpu(cpu, cp->related_cpus))) {
927 policy->governor = cp->governor;
928 found = 1;
929 break;
930 }
931 }
932#endif
933 if (!found)
934 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 /* call driver. From then on the cpufreq must be able
936 * to accept all calls to ->verify and ->setpolicy for this CPU
937 */
938 ret = cpufreq_driver->init(policy);
939 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200940 pr_debug("initialization failed\n");
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400941 goto err_unlock_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
Mike Chan187d9f42008-12-04 12:19:17 -0800943 policy->user_policy.min = policy->min;
944 policy->user_policy.max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Thomas Renningera1531ac2008-07-29 22:32:58 -0700946 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
947 CPUFREQ_START, policy);
948
Dave Jonesecf7e462009-07-08 18:48:47 -0400949 ret = cpufreq_add_dev_policy(cpu, policy, sys_dev);
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200950 if (ret) {
951 if (ret > 0)
952 /* This is a managed cpu, symlink created,
953 exit with 0 */
954 ret = 0;
Dave Jonesecf7e462009-07-08 18:48:47 -0400955 goto err_unlock_policy;
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Dave Jones909a6942009-07-08 18:05:42 -0400958 ret = cpufreq_add_dev_interface(cpu, policy, sys_dev);
Dave Jones19d6f7e2009-07-08 17:35:39 -0400959 if (ret)
960 goto err_out_unregister;
Dave Jones8ff69732006-03-05 03:37:23 -0500961
Lothar Waßmanndca02612008-05-29 17:54:52 +0200962 unlock_policy_rwsem_write(cpu);
963
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -0400964 kobject_uevent(&policy->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 module_put(cpufreq_driver->owner);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200966 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -0500967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return 0;
969
970
971err_out_unregister:
972 spin_lock_irqsave(&cpufreq_driver_lock, flags);
Rusty Russell835481d2009-01-04 05:18:06 -0800973 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -0700974 per_cpu(cpufreq_cpu_data, j) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
976
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800977 kobject_put(&policy->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 wait_for_completion(&policy->kobj_unregister);
979
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400980err_unlock_policy:
Dave Jones45709112008-03-05 14:07:34 -0500981 unlock_policy_rwsem_write(cpu);
Xiaotian Fengcad70a62010-07-20 20:11:02 +0800982 free_cpumask_var(policy->related_cpus);
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400983err_free_cpumask:
984 free_cpumask_var(policy->cpus);
985err_free_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 kfree(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987nomem_out:
988 module_put(cpufreq_driver->owner);
Ashok Rajc32b6b82005-10-30 14:59:54 -0800989module_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 return ret;
991}
992
993
994/**
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800995 * __cpufreq_remove_dev - remove a CPU device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 *
997 * Removes the cpufreq interface for a CPU device.
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800998 * Caller should already have policy_rwsem in write mode for this CPU.
999 * This routine frees the rwsem before returning.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 */
Dave Jones905d77c2008-03-05 14:28:32 -05001001static int __cpufreq_remove_dev(struct sys_device *sys_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
1003 unsigned int cpu = sys_dev->id;
1004 unsigned long flags;
1005 struct cpufreq_policy *data;
Amerigo Wang499bca92010-03-04 03:23:46 -05001006 struct kobject *kobj;
1007 struct completion *cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008#ifdef CONFIG_SMP
Grant Coadye738cf62005-11-21 21:32:28 -08001009 struct sys_device *cpu_sys_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 unsigned int j;
1011#endif
1012
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001013 pr_debug("unregistering CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 spin_lock_irqsave(&cpufreq_driver_lock, flags);
Mike Travis7a6aedf2008-03-25 15:06:53 -07001016 data = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018 if (!data) {
1019 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001020 unlock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return -EINVAL;
1022 }
Mike Travis7a6aedf2008-03-25 15:06:53 -07001023 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025
1026#ifdef CONFIG_SMP
1027 /* if this isn't the CPU which is the parent of the kobj, we
Dave Jones32ee8c32006-02-28 00:43:23 -05001028 * only need to unlink, put and exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 */
1030 if (unlikely(cpu != data->cpu)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001031 pr_debug("removing link\n");
Rusty Russell835481d2009-01-04 05:18:06 -08001032 cpumask_clear_cpu(cpu, data->cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Amerigo Wang499bca92010-03-04 03:23:46 -05001034 kobj = &sys_dev->kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 cpufreq_cpu_put(data);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001036 unlock_policy_rwsem_write(cpu);
Amerigo Wang499bca92010-03-04 03:23:46 -05001037 sysfs_remove_link(kobj, "cpufreq");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return 0;
1039 }
1040#endif
1041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042#ifdef CONFIG_SMP
Thomas Renninger084f3492007-07-09 11:35:28 -07001043
1044#ifdef CONFIG_HOTPLUG_CPU
Dmitry Monakhove77b89f2009-10-05 00:38:55 +04001045 strncpy(per_cpu(cpufreq_cpu_governor, cpu), data->governor->name,
1046 CPUFREQ_NAME_LEN);
Thomas Renninger084f3492007-07-09 11:35:28 -07001047#endif
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 /* if we have other CPUs still registered, we need to unlink them,
1050 * or else wait_for_completion below will lock up. Clean the
Mike Travis7a6aedf2008-03-25 15:06:53 -07001051 * per_cpu(cpufreq_cpu_data) while holding the lock, and remove
1052 * the sysfs links afterwards.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 */
Rusty Russell835481d2009-01-04 05:18:06 -08001054 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1055 for_each_cpu(j, data->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (j == cpu)
1057 continue;
Mike Travis7a6aedf2008-03-25 15:06:53 -07001058 per_cpu(cpufreq_cpu_data, j) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
1060 }
1061
1062 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1063
Rusty Russell835481d2009-01-04 05:18:06 -08001064 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1065 for_each_cpu(j, data->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 if (j == cpu)
1067 continue;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001068 pr_debug("removing link for cpu %u\n", j);
Thomas Renninger084f3492007-07-09 11:35:28 -07001069#ifdef CONFIG_HOTPLUG_CPU
Dmitry Monakhove77b89f2009-10-05 00:38:55 +04001070 strncpy(per_cpu(cpufreq_cpu_governor, j),
1071 data->governor->name, CPUFREQ_NAME_LEN);
Thomas Renninger084f3492007-07-09 11:35:28 -07001072#endif
Ashok Rajd434fca2005-10-30 14:59:52 -08001073 cpu_sys_dev = get_cpu_sysdev(j);
Amerigo Wang499bca92010-03-04 03:23:46 -05001074 kobj = &cpu_sys_dev->kobj;
1075 unlock_policy_rwsem_write(cpu);
1076 sysfs_remove_link(kobj, "cpufreq");
1077 lock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 cpufreq_cpu_put(data);
1079 }
1080 }
1081#else
1082 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1083#endif
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 if (cpufreq_driver->target)
1086 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001087
Amerigo Wang499bca92010-03-04 03:23:46 -05001088 kobj = &data->kobj;
1089 cmp = &data->kobj_unregister;
1090 unlock_policy_rwsem_write(cpu);
1091 kobject_put(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 /* we need to make sure that the underlying kobj is actually
Dave Jones32ee8c32006-02-28 00:43:23 -05001094 * not referenced anymore by anybody before we proceed with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 * unloading.
1096 */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001097 pr_debug("waiting for dropping of refcount\n");
Amerigo Wang499bca92010-03-04 03:23:46 -05001098 wait_for_completion(cmp);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001099 pr_debug("wait complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Amerigo Wang499bca92010-03-04 03:23:46 -05001101 lock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 if (cpufreq_driver->exit)
1103 cpufreq_driver->exit(data);
venkatesh.pallipadi@intel.com7d26e2d2009-07-02 17:08:30 -07001104 unlock_policy_rwsem_write(cpu);
1105
Jacob Shin27ecddc2011-04-27 13:32:11 -05001106#ifdef CONFIG_HOTPLUG_CPU
1107 /* when the CPU which is the parent of the kobj is hotplugged
1108 * offline, check for siblings, and create cpufreq sysfs interface
1109 * and symlinks
1110 */
1111 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1112 /* first sibling now owns the new sysfs dir */
1113 cpumask_clear_cpu(cpu, data->cpus);
1114 cpufreq_add_dev(get_cpu_sysdev(cpumask_first(data->cpus)));
1115
1116 /* finally remove our own symlink */
1117 lock_policy_rwsem_write(cpu);
1118 __cpufreq_remove_dev(sys_dev);
1119 }
1120#endif
1121
Rusty Russell835481d2009-01-04 05:18:06 -08001122 free_cpumask_var(data->related_cpus);
1123 free_cpumask_var(data->cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 kfree(data);
1125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 return 0;
1127}
1128
1129
Dave Jones905d77c2008-03-05 14:28:32 -05001130static int cpufreq_remove_dev(struct sys_device *sys_dev)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001131{
1132 unsigned int cpu = sys_dev->id;
1133 int retval;
Venki Pallipadiec282972007-03-26 12:03:19 -07001134
1135 if (cpu_is_offline(cpu))
1136 return 0;
1137
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001138 if (unlikely(lock_policy_rwsem_write(cpu)))
1139 BUG();
1140
1141 retval = __cpufreq_remove_dev(sys_dev);
1142 return retval;
1143}
1144
1145
David Howells65f27f32006-11-22 14:55:48 +00001146static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
David Howells65f27f32006-11-22 14:55:48 +00001148 struct cpufreq_policy *policy =
1149 container_of(work, struct cpufreq_policy, update);
1150 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001151 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 cpufreq_update_policy(cpu);
1153}
1154
1155/**
1156 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
1157 * @cpu: cpu number
1158 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1159 * @new_freq: CPU frequency the CPU actually runs at
1160 *
Dave Jones29464f22009-01-18 01:37:11 -05001161 * We adjust to current frequency first, and need to clean up later.
1162 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301164static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1165 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
1167 struct cpufreq_freqs freqs;
1168
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001169 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1171
1172 freqs.cpu = cpu;
1173 freqs.old = old_freq;
1174 freqs.new = new_freq;
1175 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
1176 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
1177}
1178
1179
Dave Jones32ee8c32006-02-28 00:43:23 -05001180/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301181 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001182 * @cpu: CPU number
1183 *
1184 * This is the last known freq, without actually getting it from the driver.
1185 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1186 */
1187unsigned int cpufreq_quick_get(unsigned int cpu)
1188{
1189 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301190 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001191
1192 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301193 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001194 cpufreq_cpu_put(policy);
1195 }
1196
Dave Jones4d34a672008-02-07 16:33:49 -05001197 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001198}
1199EXPORT_SYMBOL(cpufreq_quick_get);
1200
Jesse Barnes3d737102011-06-28 10:59:12 -07001201/**
1202 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1203 * @cpu: CPU number
1204 *
1205 * Just return the max possible frequency for a given CPU.
1206 */
1207unsigned int cpufreq_quick_get_max(unsigned int cpu)
1208{
1209 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1210 unsigned int ret_freq = 0;
1211
1212 if (policy) {
1213 ret_freq = policy->max;
1214 cpufreq_cpu_put(policy);
1215 }
1216
1217 return ret_freq;
1218}
1219EXPORT_SYMBOL(cpufreq_quick_get_max);
1220
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001221
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001222static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001224 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301225 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001228 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301230 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301232 if (ret_freq && policy->cur &&
1233 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1234 /* verify no discrepancy between actual and
1235 saved value exists */
1236 if (unlikely(ret_freq != policy->cur)) {
1237 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 schedule_work(&policy->update);
1239 }
1240 }
1241
Dave Jones4d34a672008-02-07 16:33:49 -05001242 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001243}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001245/**
1246 * cpufreq_get - get the current CPU frequency (in kHz)
1247 * @cpu: CPU number
1248 *
1249 * Get the CPU current (static) CPU frequency
1250 */
1251unsigned int cpufreq_get(unsigned int cpu)
1252{
1253 unsigned int ret_freq = 0;
1254 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1255
1256 if (!policy)
1257 goto out;
1258
1259 if (unlikely(lock_policy_rwsem_read(cpu)))
1260 goto out_policy;
1261
1262 ret_freq = __cpufreq_get(cpu);
1263
1264 unlock_policy_rwsem_read(cpu);
1265
1266out_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 cpufreq_cpu_put(policy);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001268out:
Dave Jones4d34a672008-02-07 16:33:49 -05001269 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271EXPORT_SYMBOL(cpufreq_get);
1272
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001273static struct sysdev_driver cpufreq_sysdev_driver = {
1274 .add = cpufreq_add_dev,
1275 .remove = cpufreq_remove_dev,
1276};
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001280 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1281 *
1282 * This function is only executed for the boot processor. The other CPUs
1283 * have been put offline by means of CPU hotplug.
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001284 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001285static int cpufreq_bp_suspend(void)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001286{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301287 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001288
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001289 int cpu = smp_processor_id();
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001290 struct cpufreq_policy *cpu_policy;
1291
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001292 pr_debug("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001293
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001294 /* If there's no policy for the boot CPU, we have nothing to do. */
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001295 cpu_policy = cpufreq_cpu_get(cpu);
1296 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001297 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001298
1299 if (cpufreq_driver->suspend) {
Rafael J. Wysocki7ca64e22011-03-10 21:13:05 +01001300 ret = cpufreq_driver->suspend(cpu_policy);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001301 if (ret)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001302 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1303 "step on CPU %u\n", cpu_policy->cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001304 }
1305
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001306 cpufreq_cpu_put(cpu_policy);
Dave Jonesc9060492008-02-07 16:32:18 -05001307 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001308}
1309
1310/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001311 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 *
1313 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001314 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1315 * restored. It will verify that the current freq is in sync with
1316 * what we believe it to be. This is a bit later than when it
1317 * should be, but nonethteless it's better than calling
1318 * cpufreq_driver->get() here which might re-enable interrupts...
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001319 *
1320 * This function is only executed for the boot CPU. The other CPUs have not
1321 * been turned on yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001323static void cpufreq_bp_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301325 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001326
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001327 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 struct cpufreq_policy *cpu_policy;
1329
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001330 pr_debug("resuming cpu %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001332 /* If there's no policy for the boot CPU, we have nothing to do. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 cpu_policy = cpufreq_cpu_get(cpu);
1334 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001335 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 if (cpufreq_driver->resume) {
1338 ret = cpufreq_driver->resume(cpu_policy);
1339 if (ret) {
1340 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1341 "step on CPU %u\n", cpu_policy->cpu);
Dave Jonesc9060492008-02-07 16:32:18 -05001342 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
1344 }
1345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 schedule_work(&cpu_policy->update);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001347
Dave Jonesc9060492008-02-07 16:32:18 -05001348fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350}
1351
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001352static struct syscore_ops cpufreq_syscore_ops = {
1353 .suspend = cpufreq_bp_suspend,
1354 .resume = cpufreq_bp_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355};
1356
1357
1358/*********************************************************************
1359 * NOTIFIER LISTS INTERFACE *
1360 *********************************************************************/
1361
1362/**
1363 * cpufreq_register_notifier - register a driver with cpufreq
1364 * @nb: notifier function to register
1365 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1366 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001367 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 * are notified about clock rate changes (once before and once after
1369 * the transition), or a list of drivers that are notified about
1370 * changes in cpufreq policy.
1371 *
1372 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001373 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 */
1375int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1376{
1377 int ret;
1378
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001379 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 switch (list) {
1382 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001383 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001384 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 break;
1386 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001387 ret = blocking_notifier_chain_register(
1388 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 break;
1390 default:
1391 ret = -EINVAL;
1392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 return ret;
1395}
1396EXPORT_SYMBOL(cpufreq_register_notifier);
1397
1398
1399/**
1400 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1401 * @nb: notifier block to be unregistered
1402 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1403 *
1404 * Remove a driver from the CPU frequency notifier list.
1405 *
1406 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001407 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 */
1409int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1410{
1411 int ret;
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 switch (list) {
1414 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001415 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001416 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 break;
1418 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001419 ret = blocking_notifier_chain_unregister(
1420 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 break;
1422 default:
1423 ret = -EINVAL;
1424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426 return ret;
1427}
1428EXPORT_SYMBOL(cpufreq_unregister_notifier);
1429
1430
1431/*********************************************************************
1432 * GOVERNORS *
1433 *********************************************************************/
1434
1435
1436int __cpufreq_driver_target(struct cpufreq_policy *policy,
1437 unsigned int target_freq,
1438 unsigned int relation)
1439{
1440 int retval = -EINVAL;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001441
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001442 pr_debug("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 target_freq, relation);
1444 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1445 retval = cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 return retval;
1448}
1449EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451int cpufreq_driver_target(struct cpufreq_policy *policy,
1452 unsigned int target_freq,
1453 unsigned int relation)
1454{
Julia Lawallf1829e42008-07-25 22:44:53 +02001455 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 policy = cpufreq_cpu_get(policy->cpu);
1458 if (!policy)
Julia Lawallf1829e42008-07-25 22:44:53 +02001459 goto no_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001461 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
Julia Lawallf1829e42008-07-25 22:44:53 +02001462 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
1464 ret = __cpufreq_driver_target(policy, target_freq, relation);
1465
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001466 unlock_policy_rwsem_write(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Julia Lawallf1829e42008-07-25 22:44:53 +02001468fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 cpufreq_cpu_put(policy);
Julia Lawallf1829e42008-07-25 22:44:53 +02001470no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 return ret;
1472}
1473EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1474
venkatesh.pallipadi@intel.combf0b90e2008-08-04 11:59:07 -07001475int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu)
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001476{
1477 int ret = 0;
1478
1479 policy = cpufreq_cpu_get(policy->cpu);
1480 if (!policy)
1481 return -EINVAL;
1482
venkatesh.pallipadi@intel.combf0b90e2008-08-04 11:59:07 -07001483 if (cpu_online(cpu) && cpufreq_driver->getavg)
1484 ret = cpufreq_driver->getavg(policy, cpu);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001485
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001486 cpufreq_cpu_put(policy);
1487 return ret;
1488}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001489EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001490
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001491/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001492 * when "event" is CPUFREQ_GOV_LIMITS
1493 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301495static int __cpufreq_governor(struct cpufreq_policy *policy,
1496 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
Dave Jonescc993ca2005-07-28 09:43:56 -07001498 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001499
1500 /* Only must be defined when default governor is known to have latency
1501 restrictions, like e.g. conservative or ondemand.
1502 That this is the case is already ensured in Kconfig
1503 */
1504#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1505 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1506#else
1507 struct cpufreq_governor *gov = NULL;
1508#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001509
1510 if (policy->governor->max_transition_latency &&
1511 policy->cpuinfo.transition_latency >
1512 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07001513 if (!gov)
1514 return -EINVAL;
1515 else {
1516 printk(KERN_WARNING "%s governor failed, too long"
1517 " transition latency of HW, fallback"
1518 " to %s governor\n",
1519 policy->governor->name,
1520 gov->name);
1521 policy->governor = gov;
1522 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
1525 if (!try_module_get(policy->governor->owner))
1526 return -EINVAL;
1527
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001528 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301529 policy->cpu, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 ret = policy->governor->governor(policy, event);
1531
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301532 /* we keep one module reference alive for
1533 each CPU governed by this CPU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 if ((event != CPUFREQ_GOV_START) || ret)
1535 module_put(policy->governor->owner);
1536 if ((event == CPUFREQ_GOV_STOP) && !ret)
1537 module_put(policy->governor->owner);
1538
1539 return ret;
1540}
1541
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543int cpufreq_register_governor(struct cpufreq_governor *governor)
1544{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001545 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
1547 if (!governor)
1548 return -EINVAL;
1549
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001550 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001551
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001552 err = -EBUSY;
1553 if (__find_governor(governor->name) == NULL) {
1554 err = 0;
1555 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Dave Jones32ee8c32006-02-28 00:43:23 -05001558 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001559 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
1561EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1562
1563
1564void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1565{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001566#ifdef CONFIG_HOTPLUG_CPU
1567 int cpu;
1568#endif
1569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 if (!governor)
1571 return;
1572
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001573#ifdef CONFIG_HOTPLUG_CPU
1574 for_each_present_cpu(cpu) {
1575 if (cpu_online(cpu))
1576 continue;
1577 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1578 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1579 }
1580#endif
1581
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001582 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001584 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 return;
1586}
1587EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1588
1589
1590
1591/*********************************************************************
1592 * POLICY INTERFACE *
1593 *********************************************************************/
1594
1595/**
1596 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05001597 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1598 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 *
1600 * Reads the current cpufreq policy.
1601 */
1602int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1603{
1604 struct cpufreq_policy *cpu_policy;
1605 if (!policy)
1606 return -EINVAL;
1607
1608 cpu_policy = cpufreq_cpu_get(cpu);
1609 if (!cpu_policy)
1610 return -EINVAL;
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
1614 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 return 0;
1616}
1617EXPORT_SYMBOL(cpufreq_get_policy);
1618
1619
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001620/*
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301621 * data : current policy.
1622 * policy : policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001623 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301624static int __cpufreq_set_policy(struct cpufreq_policy *data,
1625 struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
1627 int ret = 0;
1628
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001629 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 policy->min, policy->max);
1631
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301632 memcpy(&policy->cpuinfo, &data->cpuinfo,
1633 sizeof(struct cpufreq_cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Yi Yang53391fa2008-01-30 13:33:34 +01001635 if (policy->min > data->max || policy->max < data->min) {
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02001636 ret = -EINVAL;
1637 goto error_out;
1638 }
1639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 /* verify the cpu speed can be set within this limit */
1641 ret = cpufreq_driver->verify(policy);
1642 if (ret)
1643 goto error_out;
1644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001646 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1647 CPUFREQ_ADJUST, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
1649 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001650 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1651 CPUFREQ_INCOMPATIBLE, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
1653 /* verify the cpu speed can be set within this limit,
1654 which might be different to the first one */
1655 ret = cpufreq_driver->verify(policy);
Alan Sterne041c682006-03-27 01:16:30 -08001656 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
1659 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001660 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1661 CPUFREQ_NOTIFY, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Dave Jones7d5e3502006-02-02 17:03:42 -05001663 data->min = policy->min;
1664 data->max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001666 pr_debug("new min and max freqs are %u - %u kHz\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301667 data->min, data->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
1669 if (cpufreq_driver->setpolicy) {
1670 data->policy = policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001671 pr_debug("setting range\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 ret = cpufreq_driver->setpolicy(policy);
1673 } else {
1674 if (policy->governor != data->governor) {
1675 /* save old, working values */
1676 struct cpufreq_governor *old_gov = data->governor;
1677
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001678 pr_debug("governor switch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 /* end old governor */
Andrej Gelenbergffe62752010-05-14 15:15:58 -07001681 if (data->governor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1683
1684 /* start new governor */
1685 data->governor = policy->governor;
1686 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1687 /* new governor failed, so re-start old one */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001688 pr_debug("starting governor %s failed\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301689 data->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 if (old_gov) {
1691 data->governor = old_gov;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301692 __cpufreq_governor(data,
1693 CPUFREQ_GOV_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 }
1695 ret = -EINVAL;
1696 goto error_out;
1697 }
1698 /* might be a policy change, too, so fall through */
1699 }
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001700 pr_debug("governor: change or update limits\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1702 }
1703
Dave Jones7d5e3502006-02-02 17:03:42 -05001704error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 return ret;
1706}
1707
1708/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1710 * @cpu: CPU which shall be re-evaluated
1711 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001712 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 * at different times.
1714 */
1715int cpufreq_update_policy(unsigned int cpu)
1716{
1717 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1718 struct cpufreq_policy policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02001719 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Julia Lawallf1829e42008-07-25 22:44:53 +02001721 if (!data) {
1722 ret = -ENODEV;
1723 goto no_policy;
1724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Julia Lawallf1829e42008-07-25 22:44:53 +02001726 if (unlikely(lock_policy_rwsem_write(cpu))) {
1727 ret = -EINVAL;
1728 goto fail;
1729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001731 pr_debug("updating policy for CPU %u\n", cpu);
Dave Jones7d5e3502006-02-02 17:03:42 -05001732 memcpy(&policy, data, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 policy.min = data->user_policy.min;
1734 policy.max = data->user_policy.max;
1735 policy.policy = data->user_policy.policy;
1736 policy.governor = data->user_policy.governor;
1737
Thomas Renninger0961dd02006-01-26 18:46:33 +01001738 /* BIOS might change freq behind our back
1739 -> ask driver for current freq and notify governors about a change */
1740 if (cpufreq_driver->get) {
1741 policy.cur = cpufreq_driver->get(cpu);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001742 if (!data->cur) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001743 pr_debug("Driver did not initialize current freq");
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001744 data->cur = policy.cur;
1745 } else {
1746 if (data->cur != policy.cur)
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301747 cpufreq_out_of_sync(cpu, data->cur,
1748 policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001749 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01001750 }
1751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 ret = __cpufreq_set_policy(data, &policy);
1753
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001754 unlock_policy_rwsem_write(cpu);
1755
Julia Lawallf1829e42008-07-25 22:44:53 +02001756fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 cpufreq_cpu_put(data);
Julia Lawallf1829e42008-07-25 22:44:53 +02001758no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 return ret;
1760}
1761EXPORT_SYMBOL(cpufreq_update_policy);
1762
Satyam Sharmadd184a02007-10-02 13:28:14 -07001763static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08001764 unsigned long action, void *hcpu)
1765{
1766 unsigned int cpu = (unsigned long)hcpu;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001767 struct sys_device *sys_dev;
1768
1769 sys_dev = get_cpu_sysdev(cpu);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001770 if (sys_dev) {
1771 switch (action) {
1772 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001773 case CPU_ONLINE_FROZEN:
Ashok Rajc32b6b82005-10-30 14:59:54 -08001774 cpufreq_add_dev(sys_dev);
1775 break;
1776 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001777 case CPU_DOWN_PREPARE_FROZEN:
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001778 if (unlikely(lock_policy_rwsem_write(cpu)))
1779 BUG();
1780
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001781 __cpufreq_remove_dev(sys_dev);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001782 break;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001783 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001784 case CPU_DOWN_FAILED_FROZEN:
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001785 cpufreq_add_dev(sys_dev);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001786 break;
1787 }
1788 }
1789 return NOTIFY_OK;
1790}
1791
Neal Buckendahl9c36f742010-06-22 22:02:44 -05001792static struct notifier_block __refdata cpufreq_cpu_notifier = {
Ashok Rajc32b6b82005-10-30 14:59:54 -08001793 .notifier_call = cpufreq_cpu_callback,
1794};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796/*********************************************************************
1797 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1798 *********************************************************************/
1799
1800/**
1801 * cpufreq_register_driver - register a CPU Frequency driver
1802 * @driver_data: A struct cpufreq_driver containing the values#
1803 * submitted by the CPU Frequency driver.
1804 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001805 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05001807 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 *
1809 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001810int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
1812 unsigned long flags;
1813 int ret;
1814
1815 if (!driver_data || !driver_data->verify || !driver_data->init ||
1816 ((!driver_data->setpolicy) && (!driver_data->target)))
1817 return -EINVAL;
1818
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001819 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
1821 if (driver_data->setpolicy)
1822 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1823
1824 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1825 if (cpufreq_driver) {
1826 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1827 return -EBUSY;
1828 }
1829 cpufreq_driver = driver_data;
1830 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1831
Mike Travis7a6aedf2008-03-25 15:06:53 -07001832 ret = sysdev_driver_register(&cpu_sysdev_class,
1833 &cpufreq_sysdev_driver);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001834 if (ret)
1835 goto err_null_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001837 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 int i;
1839 ret = -ENODEV;
1840
1841 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07001842 for (i = 0; i < nr_cpu_ids; i++)
1843 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07001845 break;
1846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
1848 /* if all ->init() calls failed, unregister */
1849 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001850 pr_debug("no CPU initialized for driver %s\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301851 driver_data->name);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001852 goto err_sysdev_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 }
1854 }
1855
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001856 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001857 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001859 return 0;
1860err_sysdev_unreg:
1861 sysdev_driver_unregister(&cpu_sysdev_class,
1862 &cpufreq_sysdev_driver);
1863err_null_driver:
1864 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1865 cpufreq_driver = NULL;
1866 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05001867 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868}
1869EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1870
1871
1872/**
1873 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1874 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001875 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 * the right to do so, i.e. if you have succeeded in initialising before!
1877 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1878 * currently not initialised.
1879 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001880int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
1882 unsigned long flags;
1883
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001884 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001887 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
1889 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001890 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1893 cpufreq_driver = NULL;
1894 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1895
1896 return 0;
1897}
1898EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001899
1900static int __init cpufreq_core_init(void)
1901{
1902 int cpu;
1903
1904 for_each_possible_cpu(cpu) {
Tejun Heof1625062009-10-29 22:34:13 +09001905 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001906 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
1907 }
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02001908
1909 cpufreq_global_kobject = kobject_create_and_add("cpufreq",
1910 &cpu_sysdev_class.kset.kobj);
1911 BUG_ON(!cpufreq_global_kobject);
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001912 register_syscore_ops(&cpufreq_syscore_ops);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02001913
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001914 return 0;
1915}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001916core_initcall(cpufreq_core_init);