blob: 1e08af43ae72b4ad415ce2f9bdfd3e3faf597f35 [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 }
207 if ((val == CPUFREQ_PRECHANGE && ci->old < ci->new) ||
208 (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) ||
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700209 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530210 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
211 ci->new);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200212 pr_debug("scaling loops_per_jiffy to %lu "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530213 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215}
216#else
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530217static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
218{
219 return;
220}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221#endif
222
223
224/**
Dave Jonese4472cb2006-01-31 15:53:55 -0800225 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
226 * on frequency transition.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 *
Dave Jonese4472cb2006-01-31 15:53:55 -0800228 * This function calls the transition notifiers and the "adjust_jiffies"
229 * function. It is called twice on all CPU frequency changes that have
Dave Jones32ee8c32006-02-28 00:43:23 -0500230 * external effects.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 */
232void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
233{
Dave Jonese4472cb2006-01-31 15:53:55 -0800234 struct cpufreq_policy *policy;
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 BUG_ON(irqs_disabled());
237
238 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200239 pr_debug("notification %u of frequency transition to %u kHz\n",
Dave Jonese4472cb2006-01-31 15:53:55 -0800240 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Mike Travis7a6aedf2008-03-25 15:06:53 -0700242 policy = per_cpu(cpufreq_cpu_data, freqs->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500246 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800247 * which is not equal to what the cpufreq core thinks is
248 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 */
250 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800251 if ((policy) && (policy->cpu == freqs->cpu) &&
252 (policy->cur) && (policy->cur != freqs->old)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200253 pr_debug("Warning: CPU frequency is"
Dave Jonese4472cb2006-01-31 15:53:55 -0800254 " %u, cpufreq assumed %u kHz.\n",
255 freqs->old, policy->cur);
256 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700259 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800260 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
262 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 case CPUFREQ_POSTCHANGE:
265 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200266 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
Thomas Renninger6f4f2722010-04-20 13:17:36 +0200267 (unsigned long)freqs->cpu);
268 trace_power_frequency(POWER_PSTATE, freqs->new, freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100269 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700270 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800271 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800272 if (likely(policy) && likely(policy->cpu == freqs->cpu))
273 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 break;
275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
278
279
280
281/*********************************************************************
282 * SYSFS INTERFACE *
283 *********************************************************************/
284
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700285static struct cpufreq_governor *__find_governor(const char *str_governor)
286{
287 struct cpufreq_governor *t;
288
289 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
Dave Jones29464f22009-01-18 01:37:11 -0500290 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700291 return t;
292
293 return NULL;
294}
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296/**
297 * cpufreq_parse_governor - parse a governor string
298 */
Dave Jones905d77c2008-03-05 14:28:32 -0500299static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 struct cpufreq_governor **governor)
301{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700302 int err = -EINVAL;
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700305 goto out;
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (cpufreq_driver->setpolicy) {
308 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
309 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700310 err = 0;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530311 } else if (!strnicmp(str_governor, "powersave",
312 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700314 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700316 } else if (cpufreq_driver->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700318
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800319 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700320
321 t = __find_governor(str_governor);
322
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700323 if (t == NULL) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530324 char *name = kasprintf(GFP_KERNEL, "cpufreq_%s",
325 str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700326
327 if (name) {
328 int ret;
329
330 mutex_unlock(&cpufreq_governor_mutex);
Chris Wright326f6a52008-06-06 21:26:02 -0700331 ret = request_module("%s", name);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700332 mutex_lock(&cpufreq_governor_mutex);
333
334 if (ret == 0)
335 t = __find_governor(str_governor);
336 }
337
338 kfree(name);
339 }
340
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700341 if (t != NULL) {
342 *governor = t;
343 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700345
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800346 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
Dave Jones29464f22009-01-18 01:37:11 -0500348out:
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700349 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530354 * cpufreq_per_cpu_attr_read() / show_##file_name() -
355 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 *
357 * Write out information from cpufreq_driver->policy[cpu]; object must be
358 * "unsigned int".
359 */
360
Dave Jones32ee8c32006-02-28 00:43:23 -0500361#define show_one(file_name, object) \
362static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500363(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500364{ \
Dave Jones29464f22009-01-18 01:37:11 -0500365 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
368show_one(cpuinfo_min_freq, cpuinfo.min_freq);
369show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100370show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371show_one(scaling_min_freq, min);
372show_one(scaling_max_freq, max);
373show_one(scaling_cur_freq, cur);
374
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530375static int __cpufreq_set_policy(struct cpufreq_policy *data,
376 struct cpufreq_policy *policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378/**
379 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
380 */
381#define store_one(file_name, object) \
382static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500383(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{ \
385 unsigned int ret = -EINVAL; \
386 struct cpufreq_policy new_policy; \
387 \
388 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
389 if (ret) \
390 return -EINVAL; \
391 \
Dave Jones29464f22009-01-18 01:37:11 -0500392 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (ret != 1) \
394 return -EINVAL; \
395 \
Thomas Renninger7970e082006-04-13 15:14:04 +0200396 ret = __cpufreq_set_policy(policy, &new_policy); \
397 policy->user_policy.object = policy->object; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 \
399 return ret ? ret : count; \
400}
401
Dave Jones29464f22009-01-18 01:37:11 -0500402store_one(scaling_min_freq, min);
403store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405/**
406 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
407 */
Dave Jones905d77c2008-03-05 14:28:32 -0500408static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
409 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800411 unsigned int cur_freq = __cpufreq_get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (!cur_freq)
413 return sprintf(buf, "<unknown>");
414 return sprintf(buf, "%u\n", cur_freq);
415}
416
417
418/**
419 * show_scaling_governor - show the current policy for the specified CPU
420 */
Dave Jones905d77c2008-03-05 14:28:32 -0500421static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Dave Jones29464f22009-01-18 01:37:11 -0500423 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return sprintf(buf, "powersave\n");
425 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
426 return sprintf(buf, "performance\n");
427 else if (policy->governor)
Dave Jones29464f22009-01-18 01:37:11 -0500428 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n",
429 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return -EINVAL;
431}
432
433
434/**
435 * store_scaling_governor - store policy for the specified CPU
436 */
Dave Jones905d77c2008-03-05 14:28:32 -0500437static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
438 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
440 unsigned int ret = -EINVAL;
441 char str_governor[16];
442 struct cpufreq_policy new_policy;
443
444 ret = cpufreq_get_policy(&new_policy, policy->cpu);
445 if (ret)
446 return ret;
447
Dave Jones29464f22009-01-18 01:37:11 -0500448 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (ret != 1)
450 return -EINVAL;
451
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530452 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
453 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return -EINVAL;
455
Thomas Renninger7970e082006-04-13 15:14:04 +0200456 /* Do not use cpufreq_set_policy here or the user_policy.max
457 will be wrongly overridden */
Thomas Renninger7970e082006-04-13 15:14:04 +0200458 ret = __cpufreq_set_policy(policy, &new_policy);
459
460 policy->user_policy.policy = policy->policy;
461 policy->user_policy.governor = policy->governor;
Thomas Renninger7970e082006-04-13 15:14:04 +0200462
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530463 if (ret)
464 return ret;
465 else
466 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
469/**
470 * show_scaling_driver - show the cpufreq driver currently loaded
471 */
Dave Jones905d77c2008-03-05 14:28:32 -0500472static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
475}
476
477/**
478 * show_scaling_available_governors - show the available CPUfreq governors
479 */
Dave Jones905d77c2008-03-05 14:28:32 -0500480static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
481 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
483 ssize_t i = 0;
484 struct cpufreq_governor *t;
485
486 if (!cpufreq_driver->target) {
487 i += sprintf(buf, "performance powersave");
488 goto out;
489 }
490
491 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
Dave Jones29464f22009-01-18 01:37:11 -0500492 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
493 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 goto out;
495 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
496 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500497out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 i += sprintf(&buf[i], "\n");
499 return i;
500}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700501
Rusty Russell835481d2009-01-04 05:18:06 -0800502static ssize_t show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
504 ssize_t i = 0;
505 unsigned int cpu;
506
Rusty Russell835481d2009-01-04 05:18:06 -0800507 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (i)
509 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
510 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
511 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500512 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514 i += sprintf(&buf[i], "\n");
515 return i;
516}
517
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700518/**
519 * show_related_cpus - show the CPUs affected by each transition even if
520 * hw coordination is in use
521 */
522static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
523{
Rusty Russell835481d2009-01-04 05:18:06 -0800524 if (cpumask_empty(policy->related_cpus))
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700525 return show_cpus(policy->cpus, buf);
526 return show_cpus(policy->related_cpus, buf);
527}
528
529/**
530 * show_affected_cpus - show the CPUs affected by each transition
531 */
532static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
533{
534 return show_cpus(policy->cpus, buf);
535}
536
Venki Pallipadi9e769882007-10-26 10:18:21 -0700537static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500538 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700539{
540 unsigned int freq = 0;
541 unsigned int ret;
542
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700543 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700544 return -EINVAL;
545
546 ret = sscanf(buf, "%u", &freq);
547 if (ret != 1)
548 return -EINVAL;
549
550 policy->governor->store_setspeed(policy, freq);
551
552 return count;
553}
554
555static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
556{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700557 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700558 return sprintf(buf, "<unsupported>\n");
559
560 return policy->governor->show_setspeed(policy, buf);
561}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Thomas Renningere2f74f32009-11-19 12:31:01 +0100563/**
564 * show_scaling_driver - show the current cpufreq HW/BIOS limitation
565 */
566static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
567{
568 unsigned int limit;
569 int ret;
570 if (cpufreq_driver->bios_limit) {
571 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
572 if (!ret)
573 return sprintf(buf, "%u\n", limit);
574 }
575 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
576}
577
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200578cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
579cpufreq_freq_attr_ro(cpuinfo_min_freq);
580cpufreq_freq_attr_ro(cpuinfo_max_freq);
581cpufreq_freq_attr_ro(cpuinfo_transition_latency);
582cpufreq_freq_attr_ro(scaling_available_governors);
583cpufreq_freq_attr_ro(scaling_driver);
584cpufreq_freq_attr_ro(scaling_cur_freq);
585cpufreq_freq_attr_ro(bios_limit);
586cpufreq_freq_attr_ro(related_cpus);
587cpufreq_freq_attr_ro(affected_cpus);
588cpufreq_freq_attr_rw(scaling_min_freq);
589cpufreq_freq_attr_rw(scaling_max_freq);
590cpufreq_freq_attr_rw(scaling_governor);
591cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Dave Jones905d77c2008-03-05 14:28:32 -0500593static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 &cpuinfo_min_freq.attr,
595 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100596 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 &scaling_min_freq.attr,
598 &scaling_max_freq.attr,
599 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700600 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 &scaling_governor.attr,
602 &scaling_driver.attr,
603 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700604 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 NULL
606};
607
Thomas Renninger8aa84ad2009-07-24 15:25:05 +0200608struct kobject *cpufreq_global_kobject;
609EXPORT_SYMBOL(cpufreq_global_kobject);
610
Dave Jones29464f22009-01-18 01:37:11 -0500611#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
612#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Dave Jones29464f22009-01-18 01:37:11 -0500614static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Dave Jones905d77c2008-03-05 14:28:32 -0500616 struct cpufreq_policy *policy = to_policy(kobj);
617 struct freq_attr *fattr = to_attr(attr);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500618 ssize_t ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 policy = cpufreq_cpu_get(policy->cpu);
620 if (!policy)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500621 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800622
623 if (lock_policy_rwsem_read(policy->cpu) < 0)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500624 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800625
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530626 if (fattr->show)
627 ret = fattr->show(policy, buf);
628 else
629 ret = -EIO;
630
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800631 unlock_policy_rwsem_read(policy->cpu);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500632fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 cpufreq_cpu_put(policy);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500634no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return ret;
636}
637
Dave Jones905d77c2008-03-05 14:28:32 -0500638static ssize_t store(struct kobject *kobj, struct attribute *attr,
639 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Dave Jones905d77c2008-03-05 14:28:32 -0500641 struct cpufreq_policy *policy = to_policy(kobj);
642 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500643 ssize_t ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 policy = cpufreq_cpu_get(policy->cpu);
645 if (!policy)
Dave Jonesa07530b2008-03-05 14:22:25 -0500646 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800647
648 if (lock_policy_rwsem_write(policy->cpu) < 0)
Dave Jonesa07530b2008-03-05 14:22:25 -0500649 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800650
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530651 if (fattr->store)
652 ret = fattr->store(policy, buf, count);
653 else
654 ret = -EIO;
655
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800656 unlock_policy_rwsem_write(policy->cpu);
Dave Jonesa07530b2008-03-05 14:22:25 -0500657fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 cpufreq_cpu_put(policy);
Dave Jonesa07530b2008-03-05 14:22:25 -0500659no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return ret;
661}
662
Dave Jones905d77c2008-03-05 14:28:32 -0500663static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Dave Jones905d77c2008-03-05 14:28:32 -0500665 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200666 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 complete(&policy->kobj_unregister);
668}
669
Emese Revfy52cf25d2010-01-19 02:58:23 +0100670static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 .show = show,
672 .store = store,
673};
674
675static struct kobj_type ktype_cpufreq = {
676 .sysfs_ops = &sysfs_ops,
677 .default_attrs = default_attrs,
678 .release = cpufreq_sysfs_release,
679};
680
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200681/*
682 * Returns:
683 * Negative: Failure
684 * 0: Success
685 * Positive: When we have a managed CPU and the sysfs got symlinked
686 */
Alex Chiangcf3289d02009-11-17 20:27:08 -0700687static int cpufreq_add_dev_policy(unsigned int cpu,
688 struct cpufreq_policy *policy,
689 struct sys_device *sys_dev)
Dave Jonesecf7e462009-07-08 18:48:47 -0400690{
691 int ret = 0;
692#ifdef CONFIG_SMP
693 unsigned long flags;
694 unsigned int j;
Dave Jonesecf7e462009-07-08 18:48:47 -0400695#ifdef CONFIG_HOTPLUG_CPU
Dmitry Monakhove77b89f2009-10-05 00:38:55 +0400696 struct cpufreq_governor *gov;
697
698 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
699 if (gov) {
700 policy->governor = gov;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200701 pr_debug("Restoring governor %s for cpu %d\n",
Dave Jonesecf7e462009-07-08 18:48:47 -0400702 policy->governor->name, cpu);
703 }
704#endif
705
706 for_each_cpu(j, policy->cpus) {
707 struct cpufreq_policy *managed_policy;
708
709 if (cpu == j)
710 continue;
711
712 /* Check for existing affected CPUs.
713 * They may not be aware of it due to CPU Hotplug.
714 * cpufreq_cpu_put is called when the device is removed
715 * in __cpufreq_remove_dev()
716 */
717 managed_policy = cpufreq_cpu_get(j);
718 if (unlikely(managed_policy)) {
719
720 /* Set proper policy_cpu */
721 unlock_policy_rwsem_write(cpu);
Tejun Heof1625062009-10-29 22:34:13 +0900722 per_cpu(cpufreq_policy_cpu, cpu) = managed_policy->cpu;
Dave Jonesecf7e462009-07-08 18:48:47 -0400723
724 if (lock_policy_rwsem_write(cpu) < 0) {
725 /* Should not go through policy unlock path */
726 if (cpufreq_driver->exit)
727 cpufreq_driver->exit(policy);
728 cpufreq_cpu_put(managed_policy);
729 return -EBUSY;
730 }
731
732 spin_lock_irqsave(&cpufreq_driver_lock, flags);
733 cpumask_copy(managed_policy->cpus, policy->cpus);
734 per_cpu(cpufreq_cpu_data, cpu) = managed_policy;
735 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
736
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200737 pr_debug("CPU already managed, adding link\n");
Dave Jonesecf7e462009-07-08 18:48:47 -0400738 ret = sysfs_create_link(&sys_dev->kobj,
739 &managed_policy->kobj,
740 "cpufreq");
741 if (ret)
742 cpufreq_cpu_put(managed_policy);
743 /*
744 * Success. We only needed to be added to the mask.
745 * Call driver->exit() because only the cpu parent of
746 * the kobj needed to call init().
747 */
748 if (cpufreq_driver->exit)
749 cpufreq_driver->exit(policy);
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200750
751 if (!ret)
752 return 1;
753 else
754 return ret;
Dave Jonesecf7e462009-07-08 18:48:47 -0400755 }
756 }
757#endif
758 return ret;
759}
760
761
Dave Jones19d6f7e2009-07-08 17:35:39 -0400762/* symlink affected CPUs */
Alex Chiangcf3289d02009-11-17 20:27:08 -0700763static int cpufreq_add_dev_symlink(unsigned int cpu,
764 struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400765{
766 unsigned int j;
767 int ret = 0;
768
769 for_each_cpu(j, policy->cpus) {
770 struct cpufreq_policy *managed_policy;
771 struct sys_device *cpu_sys_dev;
772
773 if (j == cpu)
774 continue;
775 if (!cpu_online(j))
776 continue;
777
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200778 pr_debug("CPU %u already managed, adding link\n", j);
Dave Jones19d6f7e2009-07-08 17:35:39 -0400779 managed_policy = cpufreq_cpu_get(cpu);
780 cpu_sys_dev = get_cpu_sysdev(j);
781 ret = sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
782 "cpufreq");
783 if (ret) {
784 cpufreq_cpu_put(managed_policy);
785 return ret;
786 }
787 }
788 return ret;
789}
790
Alex Chiangcf3289d02009-11-17 20:27:08 -0700791static int cpufreq_add_dev_interface(unsigned int cpu,
792 struct cpufreq_policy *policy,
793 struct sys_device *sys_dev)
Dave Jones909a6942009-07-08 18:05:42 -0400794{
Dave Jonesecf7e462009-07-08 18:48:47 -0400795 struct cpufreq_policy new_policy;
Dave Jones909a6942009-07-08 18:05:42 -0400796 struct freq_attr **drv_attr;
797 unsigned long flags;
798 int ret = 0;
799 unsigned int j;
800
801 /* prepare interface data */
802 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
803 &sys_dev->kobj, "cpufreq");
804 if (ret)
805 return ret;
806
807 /* set up files for this cpu device */
808 drv_attr = cpufreq_driver->attr;
809 while ((drv_attr) && (*drv_attr)) {
810 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
811 if (ret)
812 goto err_out_kobj_put;
813 drv_attr++;
814 }
815 if (cpufreq_driver->get) {
816 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
817 if (ret)
818 goto err_out_kobj_put;
819 }
820 if (cpufreq_driver->target) {
821 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
822 if (ret)
823 goto err_out_kobj_put;
824 }
Thomas Renningere2f74f32009-11-19 12:31:01 +0100825 if (cpufreq_driver->bios_limit) {
826 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
827 if (ret)
828 goto err_out_kobj_put;
829 }
Dave Jones909a6942009-07-08 18:05:42 -0400830
831 spin_lock_irqsave(&cpufreq_driver_lock, flags);
832 for_each_cpu(j, policy->cpus) {
Julia Lawallbec037a2010-08-05 22:23:00 +0200833 if (!cpu_online(j))
834 continue;
Dave Jones909a6942009-07-08 18:05:42 -0400835 per_cpu(cpufreq_cpu_data, j) = policy;
Tejun Heof1625062009-10-29 22:34:13 +0900836 per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
Dave Jones909a6942009-07-08 18:05:42 -0400837 }
838 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
839
840 ret = cpufreq_add_dev_symlink(cpu, policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400841 if (ret)
842 goto err_out_kobj_put;
843
844 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
845 /* assure that the starting sequence is run in __cpufreq_set_policy */
846 policy->governor = NULL;
847
848 /* set default policy */
849 ret = __cpufreq_set_policy(policy, &new_policy);
850 policy->user_policy.policy = policy->policy;
851 policy->user_policy.governor = policy->governor;
852
853 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200854 pr_debug("setting policy failed\n");
Dave Jonesecf7e462009-07-08 18:48:47 -0400855 if (cpufreq_driver->exit)
856 cpufreq_driver->exit(policy);
857 }
Dave Jones909a6942009-07-08 18:05:42 -0400858 return ret;
859
860err_out_kobj_put:
861 kobject_put(&policy->kobj);
862 wait_for_completion(&policy->kobj_unregister);
863 return ret;
864}
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867/**
868 * cpufreq_add_dev - add a CPU device
869 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500870 * Adds the cpufreq interface for a CPU device.
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400871 *
872 * The Oracle says: try running cpufreq registration/unregistration concurrently
873 * with with cpu hotplugging and all hell will break loose. Tried to clean this
874 * mess up, but more thorough testing is needed. - Mathieu
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 */
Dave Jones905d77c2008-03-05 14:28:32 -0500876static int cpufreq_add_dev(struct sys_device *sys_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
878 unsigned int cpu = sys_dev->id;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500879 int ret = 0, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 unsigned long flags;
882 unsigned int j;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500883#ifdef CONFIG_HOTPLUG_CPU
884 int sibling;
885#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Ashok Rajc32b6b82005-10-30 14:59:54 -0800887 if (cpu_is_offline(cpu))
888 return 0;
889
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200890 pr_debug("adding CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892#ifdef CONFIG_SMP
893 /* check whether a different CPU already registered this
894 * CPU because it is in the same boat. */
895 policy = cpufreq_cpu_get(cpu);
896 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -0500897 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return 0;
899 }
900#endif
901
902 if (!try_module_get(cpufreq_driver->owner)) {
903 ret = -EINVAL;
904 goto module_out;
905 }
906
Dave Jones059019a2009-07-08 16:30:03 -0400907 ret = -ENOMEM;
Dave Jonese98df502005-10-20 15:17:43 -0700908 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
Dave Jones059019a2009-07-08 16:30:03 -0400909 if (!policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 goto nomem_out;
Dave Jones059019a2009-07-08 16:30:03 -0400911
912 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400913 goto err_free_policy;
Dave Jones059019a2009-07-08 16:30:03 -0400914
915 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400916 goto err_free_cpumask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 policy->cpu = cpu;
Rusty Russell835481d2009-01-04 05:18:06 -0800919 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800921 /* Initially set CPU itself as the policy_cpu */
Tejun Heof1625062009-10-29 22:34:13 +0900922 per_cpu(cpufreq_policy_cpu, cpu) = cpu;
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400923 ret = (lock_policy_rwsem_write(cpu) < 0);
924 WARN_ON(ret);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 init_completion(&policy->kobj_unregister);
David Howells65f27f32006-11-22 14:55:48 +0000927 INIT_WORK(&policy->update, handle_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Thomas Renninger8122c6c2007-10-02 13:28:09 -0700929 /* Set governor before ->init, so that driver could check it */
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500930#ifdef CONFIG_HOTPLUG_CPU
931 for_each_online_cpu(sibling) {
932 struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
933 if (cp && cp->governor &&
934 (cpumask_test_cpu(cpu, cp->related_cpus))) {
935 policy->governor = cp->governor;
936 found = 1;
937 break;
938 }
939 }
940#endif
941 if (!found)
942 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 /* call driver. From then on the cpufreq must be able
944 * to accept all calls to ->verify and ->setpolicy for this CPU
945 */
946 ret = cpufreq_driver->init(policy);
947 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200948 pr_debug("initialization failed\n");
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400949 goto err_unlock_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
Mike Chan187d9f42008-12-04 12:19:17 -0800951 policy->user_policy.min = policy->min;
952 policy->user_policy.max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Thomas Renningera1531ac2008-07-29 22:32:58 -0700954 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
955 CPUFREQ_START, policy);
956
Dave Jonesecf7e462009-07-08 18:48:47 -0400957 ret = cpufreq_add_dev_policy(cpu, policy, sys_dev);
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200958 if (ret) {
959 if (ret > 0)
960 /* This is a managed cpu, symlink created,
961 exit with 0 */
962 ret = 0;
Dave Jonesecf7e462009-07-08 18:48:47 -0400963 goto err_unlock_policy;
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Dave Jones909a6942009-07-08 18:05:42 -0400966 ret = cpufreq_add_dev_interface(cpu, policy, sys_dev);
Dave Jones19d6f7e2009-07-08 17:35:39 -0400967 if (ret)
968 goto err_out_unregister;
Dave Jones8ff69732006-03-05 03:37:23 -0500969
Lothar Waßmanndca02612008-05-29 17:54:52 +0200970 unlock_policy_rwsem_write(cpu);
971
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -0400972 kobject_uevent(&policy->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 module_put(cpufreq_driver->owner);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200974 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -0500975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 return 0;
977
978
979err_out_unregister:
980 spin_lock_irqsave(&cpufreq_driver_lock, flags);
Rusty Russell835481d2009-01-04 05:18:06 -0800981 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -0700982 per_cpu(cpufreq_cpu_data, j) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
984
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800985 kobject_put(&policy->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 wait_for_completion(&policy->kobj_unregister);
987
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400988err_unlock_policy:
Dave Jones45709112008-03-05 14:07:34 -0500989 unlock_policy_rwsem_write(cpu);
Xiaotian Fengcad70a62010-07-20 20:11:02 +0800990 free_cpumask_var(policy->related_cpus);
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400991err_free_cpumask:
992 free_cpumask_var(policy->cpus);
993err_free_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 kfree(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995nomem_out:
996 module_put(cpufreq_driver->owner);
Ashok Rajc32b6b82005-10-30 14:59:54 -0800997module_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 return ret;
999}
1000
1001
1002/**
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001003 * __cpufreq_remove_dev - remove a CPU device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 *
1005 * Removes the cpufreq interface for a CPU device.
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001006 * Caller should already have policy_rwsem in write mode for this CPU.
1007 * This routine frees the rwsem before returning.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 */
Dave Jones905d77c2008-03-05 14:28:32 -05001009static int __cpufreq_remove_dev(struct sys_device *sys_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
1011 unsigned int cpu = sys_dev->id;
1012 unsigned long flags;
1013 struct cpufreq_policy *data;
Amerigo Wang499bca92010-03-04 03:23:46 -05001014 struct kobject *kobj;
1015 struct completion *cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016#ifdef CONFIG_SMP
Grant Coadye738cf62005-11-21 21:32:28 -08001017 struct sys_device *cpu_sys_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 unsigned int j;
1019#endif
1020
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001021 pr_debug("unregistering CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 spin_lock_irqsave(&cpufreq_driver_lock, flags);
Mike Travis7a6aedf2008-03-25 15:06:53 -07001024 data = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 if (!data) {
1027 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001028 unlock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 return -EINVAL;
1030 }
Mike Travis7a6aedf2008-03-25 15:06:53 -07001031 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033
1034#ifdef CONFIG_SMP
1035 /* if this isn't the CPU which is the parent of the kobj, we
Dave Jones32ee8c32006-02-28 00:43:23 -05001036 * only need to unlink, put and exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 */
1038 if (unlikely(cpu != data->cpu)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001039 pr_debug("removing link\n");
Rusty Russell835481d2009-01-04 05:18:06 -08001040 cpumask_clear_cpu(cpu, data->cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Amerigo Wang499bca92010-03-04 03:23:46 -05001042 kobj = &sys_dev->kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 cpufreq_cpu_put(data);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001044 unlock_policy_rwsem_write(cpu);
Amerigo Wang499bca92010-03-04 03:23:46 -05001045 sysfs_remove_link(kobj, "cpufreq");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 return 0;
1047 }
1048#endif
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050#ifdef CONFIG_SMP
Thomas Renninger084f3492007-07-09 11:35:28 -07001051
1052#ifdef CONFIG_HOTPLUG_CPU
Dmitry Monakhove77b89f2009-10-05 00:38:55 +04001053 strncpy(per_cpu(cpufreq_cpu_governor, cpu), data->governor->name,
1054 CPUFREQ_NAME_LEN);
Thomas Renninger084f3492007-07-09 11:35:28 -07001055#endif
1056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 /* if we have other CPUs still registered, we need to unlink them,
1058 * or else wait_for_completion below will lock up. Clean the
Mike Travis7a6aedf2008-03-25 15:06:53 -07001059 * per_cpu(cpufreq_cpu_data) while holding the lock, and remove
1060 * the sysfs links afterwards.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 */
Rusty Russell835481d2009-01-04 05:18:06 -08001062 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1063 for_each_cpu(j, data->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (j == cpu)
1065 continue;
Mike Travis7a6aedf2008-03-25 15:06:53 -07001066 per_cpu(cpufreq_cpu_data, j) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068 }
1069
1070 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1071
Rusty Russell835481d2009-01-04 05:18:06 -08001072 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1073 for_each_cpu(j, data->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 if (j == cpu)
1075 continue;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001076 pr_debug("removing link for cpu %u\n", j);
Thomas Renninger084f3492007-07-09 11:35:28 -07001077#ifdef CONFIG_HOTPLUG_CPU
Dmitry Monakhove77b89f2009-10-05 00:38:55 +04001078 strncpy(per_cpu(cpufreq_cpu_governor, j),
1079 data->governor->name, CPUFREQ_NAME_LEN);
Thomas Renninger084f3492007-07-09 11:35:28 -07001080#endif
Ashok Rajd434fca2005-10-30 14:59:52 -08001081 cpu_sys_dev = get_cpu_sysdev(j);
Amerigo Wang499bca92010-03-04 03:23:46 -05001082 kobj = &cpu_sys_dev->kobj;
1083 unlock_policy_rwsem_write(cpu);
1084 sysfs_remove_link(kobj, "cpufreq");
1085 lock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 cpufreq_cpu_put(data);
1087 }
1088 }
1089#else
1090 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1091#endif
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (cpufreq_driver->target)
1094 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001095
Amerigo Wang499bca92010-03-04 03:23:46 -05001096 kobj = &data->kobj;
1097 cmp = &data->kobj_unregister;
1098 unlock_policy_rwsem_write(cpu);
1099 kobject_put(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 /* we need to make sure that the underlying kobj is actually
Dave Jones32ee8c32006-02-28 00:43:23 -05001102 * not referenced anymore by anybody before we proceed with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 * unloading.
1104 */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001105 pr_debug("waiting for dropping of refcount\n");
Amerigo Wang499bca92010-03-04 03:23:46 -05001106 wait_for_completion(cmp);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001107 pr_debug("wait complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Amerigo Wang499bca92010-03-04 03:23:46 -05001109 lock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (cpufreq_driver->exit)
1111 cpufreq_driver->exit(data);
venkatesh.pallipadi@intel.com7d26e2d2009-07-02 17:08:30 -07001112 unlock_policy_rwsem_write(cpu);
1113
Jacob Shin27ecddc2011-04-27 13:32:11 -05001114#ifdef CONFIG_HOTPLUG_CPU
1115 /* when the CPU which is the parent of the kobj is hotplugged
1116 * offline, check for siblings, and create cpufreq sysfs interface
1117 * and symlinks
1118 */
1119 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1120 /* first sibling now owns the new sysfs dir */
1121 cpumask_clear_cpu(cpu, data->cpus);
1122 cpufreq_add_dev(get_cpu_sysdev(cpumask_first(data->cpus)));
1123
1124 /* finally remove our own symlink */
1125 lock_policy_rwsem_write(cpu);
1126 __cpufreq_remove_dev(sys_dev);
1127 }
1128#endif
1129
Rusty Russell835481d2009-01-04 05:18:06 -08001130 free_cpumask_var(data->related_cpus);
1131 free_cpumask_var(data->cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 kfree(data);
1133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return 0;
1135}
1136
1137
Dave Jones905d77c2008-03-05 14:28:32 -05001138static int cpufreq_remove_dev(struct sys_device *sys_dev)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001139{
1140 unsigned int cpu = sys_dev->id;
1141 int retval;
Venki Pallipadiec282972007-03-26 12:03:19 -07001142
1143 if (cpu_is_offline(cpu))
1144 return 0;
1145
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001146 if (unlikely(lock_policy_rwsem_write(cpu)))
1147 BUG();
1148
1149 retval = __cpufreq_remove_dev(sys_dev);
1150 return retval;
1151}
1152
1153
David Howells65f27f32006-11-22 14:55:48 +00001154static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
David Howells65f27f32006-11-22 14:55:48 +00001156 struct cpufreq_policy *policy =
1157 container_of(work, struct cpufreq_policy, update);
1158 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001159 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 cpufreq_update_policy(cpu);
1161}
1162
1163/**
1164 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
1165 * @cpu: cpu number
1166 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1167 * @new_freq: CPU frequency the CPU actually runs at
1168 *
Dave Jones29464f22009-01-18 01:37:11 -05001169 * We adjust to current frequency first, and need to clean up later.
1170 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301172static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1173 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
1175 struct cpufreq_freqs freqs;
1176
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001177 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1179
1180 freqs.cpu = cpu;
1181 freqs.old = old_freq;
1182 freqs.new = new_freq;
1183 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
1184 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
1185}
1186
1187
Dave Jones32ee8c32006-02-28 00:43:23 -05001188/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301189 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001190 * @cpu: CPU number
1191 *
1192 * This is the last known freq, without actually getting it from the driver.
1193 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1194 */
1195unsigned int cpufreq_quick_get(unsigned int cpu)
1196{
1197 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301198 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001199
1200 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301201 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001202 cpufreq_cpu_put(policy);
1203 }
1204
Dave Jones4d34a672008-02-07 16:33:49 -05001205 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001206}
1207EXPORT_SYMBOL(cpufreq_quick_get);
1208
1209
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001210static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001212 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301213 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001216 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301218 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301220 if (ret_freq && policy->cur &&
1221 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1222 /* verify no discrepancy between actual and
1223 saved value exists */
1224 if (unlikely(ret_freq != policy->cur)) {
1225 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 schedule_work(&policy->update);
1227 }
1228 }
1229
Dave Jones4d34a672008-02-07 16:33:49 -05001230 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001231}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001233/**
1234 * cpufreq_get - get the current CPU frequency (in kHz)
1235 * @cpu: CPU number
1236 *
1237 * Get the CPU current (static) CPU frequency
1238 */
1239unsigned int cpufreq_get(unsigned int cpu)
1240{
1241 unsigned int ret_freq = 0;
1242 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1243
1244 if (!policy)
1245 goto out;
1246
1247 if (unlikely(lock_policy_rwsem_read(cpu)))
1248 goto out_policy;
1249
1250 ret_freq = __cpufreq_get(cpu);
1251
1252 unlock_policy_rwsem_read(cpu);
1253
1254out_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 cpufreq_cpu_put(policy);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001256out:
Dave Jones4d34a672008-02-07 16:33:49 -05001257 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258}
1259EXPORT_SYMBOL(cpufreq_get);
1260
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001261static struct sysdev_driver cpufreq_sysdev_driver = {
1262 .add = cpufreq_add_dev,
1263 .remove = cpufreq_remove_dev,
1264};
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001268 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1269 *
1270 * This function is only executed for the boot processor. The other CPUs
1271 * have been put offline by means of CPU hotplug.
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001272 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001273static int cpufreq_bp_suspend(void)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001274{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301275 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001276
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001277 int cpu = smp_processor_id();
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001278 struct cpufreq_policy *cpu_policy;
1279
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001280 pr_debug("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001281
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001282 /* If there's no policy for the boot CPU, we have nothing to do. */
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001283 cpu_policy = cpufreq_cpu_get(cpu);
1284 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001285 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001286
1287 if (cpufreq_driver->suspend) {
Rafael J. Wysocki7ca64e22011-03-10 21:13:05 +01001288 ret = cpufreq_driver->suspend(cpu_policy);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001289 if (ret)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001290 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1291 "step on CPU %u\n", cpu_policy->cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001292 }
1293
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001294 cpufreq_cpu_put(cpu_policy);
Dave Jonesc9060492008-02-07 16:32:18 -05001295 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001296}
1297
1298/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001299 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 *
1301 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001302 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1303 * restored. It will verify that the current freq is in sync with
1304 * what we believe it to be. This is a bit later than when it
1305 * should be, but nonethteless it's better than calling
1306 * cpufreq_driver->get() here which might re-enable interrupts...
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001307 *
1308 * This function is only executed for the boot CPU. The other CPUs have not
1309 * been turned on yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001311static void cpufreq_bp_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301313 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001314
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001315 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 struct cpufreq_policy *cpu_policy;
1317
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001318 pr_debug("resuming cpu %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001320 /* If there's no policy for the boot CPU, we have nothing to do. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 cpu_policy = cpufreq_cpu_get(cpu);
1322 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001323 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 if (cpufreq_driver->resume) {
1326 ret = cpufreq_driver->resume(cpu_policy);
1327 if (ret) {
1328 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1329 "step on CPU %u\n", cpu_policy->cpu);
Dave Jonesc9060492008-02-07 16:32:18 -05001330 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
1332 }
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 schedule_work(&cpu_policy->update);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001335
Dave Jonesc9060492008-02-07 16:32:18 -05001336fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338}
1339
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001340static struct syscore_ops cpufreq_syscore_ops = {
1341 .suspend = cpufreq_bp_suspend,
1342 .resume = cpufreq_bp_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343};
1344
1345
1346/*********************************************************************
1347 * NOTIFIER LISTS INTERFACE *
1348 *********************************************************************/
1349
1350/**
1351 * cpufreq_register_notifier - register a driver with cpufreq
1352 * @nb: notifier function to register
1353 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1354 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001355 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 * are notified about clock rate changes (once before and once after
1357 * the transition), or a list of drivers that are notified about
1358 * changes in cpufreq policy.
1359 *
1360 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001361 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 */
1363int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1364{
1365 int ret;
1366
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001367 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 switch (list) {
1370 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001371 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001372 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 break;
1374 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001375 ret = blocking_notifier_chain_register(
1376 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 break;
1378 default:
1379 ret = -EINVAL;
1380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382 return ret;
1383}
1384EXPORT_SYMBOL(cpufreq_register_notifier);
1385
1386
1387/**
1388 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1389 * @nb: notifier block to be unregistered
1390 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1391 *
1392 * Remove a driver from the CPU frequency notifier list.
1393 *
1394 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001395 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 */
1397int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1398{
1399 int ret;
1400
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 switch (list) {
1402 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001403 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001404 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 break;
1406 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001407 ret = blocking_notifier_chain_unregister(
1408 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 break;
1410 default:
1411 ret = -EINVAL;
1412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 return ret;
1415}
1416EXPORT_SYMBOL(cpufreq_unregister_notifier);
1417
1418
1419/*********************************************************************
1420 * GOVERNORS *
1421 *********************************************************************/
1422
1423
1424int __cpufreq_driver_target(struct cpufreq_policy *policy,
1425 unsigned int target_freq,
1426 unsigned int relation)
1427{
1428 int retval = -EINVAL;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001429
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001430 pr_debug("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 target_freq, relation);
1432 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1433 retval = cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 return retval;
1436}
1437EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439int cpufreq_driver_target(struct cpufreq_policy *policy,
1440 unsigned int target_freq,
1441 unsigned int relation)
1442{
Julia Lawallf1829e42008-07-25 22:44:53 +02001443 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
1445 policy = cpufreq_cpu_get(policy->cpu);
1446 if (!policy)
Julia Lawallf1829e42008-07-25 22:44:53 +02001447 goto no_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001449 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
Julia Lawallf1829e42008-07-25 22:44:53 +02001450 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 ret = __cpufreq_driver_target(policy, target_freq, relation);
1453
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001454 unlock_policy_rwsem_write(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Julia Lawallf1829e42008-07-25 22:44:53 +02001456fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 cpufreq_cpu_put(policy);
Julia Lawallf1829e42008-07-25 22:44:53 +02001458no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 return ret;
1460}
1461EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1462
venkatesh.pallipadi@intel.combf0b90e2008-08-04 11:59:07 -07001463int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu)
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001464{
1465 int ret = 0;
1466
1467 policy = cpufreq_cpu_get(policy->cpu);
1468 if (!policy)
1469 return -EINVAL;
1470
venkatesh.pallipadi@intel.combf0b90e2008-08-04 11:59:07 -07001471 if (cpu_online(cpu) && cpufreq_driver->getavg)
1472 ret = cpufreq_driver->getavg(policy, cpu);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001473
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001474 cpufreq_cpu_put(policy);
1475 return ret;
1476}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001477EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001478
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001479/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001480 * when "event" is CPUFREQ_GOV_LIMITS
1481 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301483static int __cpufreq_governor(struct cpufreq_policy *policy,
1484 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
Dave Jonescc993ca2005-07-28 09:43:56 -07001486 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001487
1488 /* Only must be defined when default governor is known to have latency
1489 restrictions, like e.g. conservative or ondemand.
1490 That this is the case is already ensured in Kconfig
1491 */
1492#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1493 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1494#else
1495 struct cpufreq_governor *gov = NULL;
1496#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001497
1498 if (policy->governor->max_transition_latency &&
1499 policy->cpuinfo.transition_latency >
1500 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07001501 if (!gov)
1502 return -EINVAL;
1503 else {
1504 printk(KERN_WARNING "%s governor failed, too long"
1505 " transition latency of HW, fallback"
1506 " to %s governor\n",
1507 policy->governor->name,
1508 gov->name);
1509 policy->governor = gov;
1510 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 if (!try_module_get(policy->governor->owner))
1514 return -EINVAL;
1515
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001516 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301517 policy->cpu, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 ret = policy->governor->governor(policy, event);
1519
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301520 /* we keep one module reference alive for
1521 each CPU governed by this CPU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 if ((event != CPUFREQ_GOV_START) || ret)
1523 module_put(policy->governor->owner);
1524 if ((event == CPUFREQ_GOV_STOP) && !ret)
1525 module_put(policy->governor->owner);
1526
1527 return ret;
1528}
1529
1530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531int cpufreq_register_governor(struct cpufreq_governor *governor)
1532{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001533 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 if (!governor)
1536 return -EINVAL;
1537
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001538 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001539
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001540 err = -EBUSY;
1541 if (__find_governor(governor->name) == NULL) {
1542 err = 0;
1543 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Dave Jones32ee8c32006-02-28 00:43:23 -05001546 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001547 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
1549EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1550
1551
1552void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1553{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001554#ifdef CONFIG_HOTPLUG_CPU
1555 int cpu;
1556#endif
1557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 if (!governor)
1559 return;
1560
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001561#ifdef CONFIG_HOTPLUG_CPU
1562 for_each_present_cpu(cpu) {
1563 if (cpu_online(cpu))
1564 continue;
1565 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1566 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1567 }
1568#endif
1569
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001570 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001572 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 return;
1574}
1575EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1576
1577
1578
1579/*********************************************************************
1580 * POLICY INTERFACE *
1581 *********************************************************************/
1582
1583/**
1584 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05001585 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1586 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 *
1588 * Reads the current cpufreq policy.
1589 */
1590int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1591{
1592 struct cpufreq_policy *cpu_policy;
1593 if (!policy)
1594 return -EINVAL;
1595
1596 cpu_policy = cpufreq_cpu_get(cpu);
1597 if (!cpu_policy)
1598 return -EINVAL;
1599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
1602 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 return 0;
1604}
1605EXPORT_SYMBOL(cpufreq_get_policy);
1606
1607
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001608/*
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301609 * data : current policy.
1610 * policy : policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001611 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301612static int __cpufreq_set_policy(struct cpufreq_policy *data,
1613 struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
1615 int ret = 0;
1616
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001617 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 policy->min, policy->max);
1619
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301620 memcpy(&policy->cpuinfo, &data->cpuinfo,
1621 sizeof(struct cpufreq_cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Yi Yang53391fa2008-01-30 13:33:34 +01001623 if (policy->min > data->max || policy->max < data->min) {
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02001624 ret = -EINVAL;
1625 goto error_out;
1626 }
1627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 /* verify the cpu speed can be set within this limit */
1629 ret = cpufreq_driver->verify(policy);
1630 if (ret)
1631 goto error_out;
1632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001634 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1635 CPUFREQ_ADJUST, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001638 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1639 CPUFREQ_INCOMPATIBLE, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
1641 /* verify the cpu speed can be set within this limit,
1642 which might be different to the first one */
1643 ret = cpufreq_driver->verify(policy);
Alan Sterne041c682006-03-27 01:16:30 -08001644 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
1647 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001648 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1649 CPUFREQ_NOTIFY, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Dave Jones7d5e3502006-02-02 17:03:42 -05001651 data->min = policy->min;
1652 data->max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001654 pr_debug("new min and max freqs are %u - %u kHz\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301655 data->min, data->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657 if (cpufreq_driver->setpolicy) {
1658 data->policy = policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001659 pr_debug("setting range\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 ret = cpufreq_driver->setpolicy(policy);
1661 } else {
1662 if (policy->governor != data->governor) {
1663 /* save old, working values */
1664 struct cpufreq_governor *old_gov = data->governor;
1665
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001666 pr_debug("governor switch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
1668 /* end old governor */
Andrej Gelenbergffe62752010-05-14 15:15:58 -07001669 if (data->governor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1671
1672 /* start new governor */
1673 data->governor = policy->governor;
1674 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1675 /* new governor failed, so re-start old one */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001676 pr_debug("starting governor %s failed\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301677 data->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 if (old_gov) {
1679 data->governor = old_gov;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301680 __cpufreq_governor(data,
1681 CPUFREQ_GOV_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
1683 ret = -EINVAL;
1684 goto error_out;
1685 }
1686 /* might be a policy change, too, so fall through */
1687 }
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001688 pr_debug("governor: change or update limits\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1690 }
1691
Dave Jones7d5e3502006-02-02 17:03:42 -05001692error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 return ret;
1694}
1695
1696/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1698 * @cpu: CPU which shall be re-evaluated
1699 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001700 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 * at different times.
1702 */
1703int cpufreq_update_policy(unsigned int cpu)
1704{
1705 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1706 struct cpufreq_policy policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02001707 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Julia Lawallf1829e42008-07-25 22:44:53 +02001709 if (!data) {
1710 ret = -ENODEV;
1711 goto no_policy;
1712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Julia Lawallf1829e42008-07-25 22:44:53 +02001714 if (unlikely(lock_policy_rwsem_write(cpu))) {
1715 ret = -EINVAL;
1716 goto fail;
1717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001719 pr_debug("updating policy for CPU %u\n", cpu);
Dave Jones7d5e3502006-02-02 17:03:42 -05001720 memcpy(&policy, data, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 policy.min = data->user_policy.min;
1722 policy.max = data->user_policy.max;
1723 policy.policy = data->user_policy.policy;
1724 policy.governor = data->user_policy.governor;
1725
Thomas Renninger0961dd02006-01-26 18:46:33 +01001726 /* BIOS might change freq behind our back
1727 -> ask driver for current freq and notify governors about a change */
1728 if (cpufreq_driver->get) {
1729 policy.cur = cpufreq_driver->get(cpu);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001730 if (!data->cur) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001731 pr_debug("Driver did not initialize current freq");
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001732 data->cur = policy.cur;
1733 } else {
1734 if (data->cur != policy.cur)
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301735 cpufreq_out_of_sync(cpu, data->cur,
1736 policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001737 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01001738 }
1739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 ret = __cpufreq_set_policy(data, &policy);
1741
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001742 unlock_policy_rwsem_write(cpu);
1743
Julia Lawallf1829e42008-07-25 22:44:53 +02001744fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 cpufreq_cpu_put(data);
Julia Lawallf1829e42008-07-25 22:44:53 +02001746no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 return ret;
1748}
1749EXPORT_SYMBOL(cpufreq_update_policy);
1750
Satyam Sharmadd184a02007-10-02 13:28:14 -07001751static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08001752 unsigned long action, void *hcpu)
1753{
1754 unsigned int cpu = (unsigned long)hcpu;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001755 struct sys_device *sys_dev;
1756
1757 sys_dev = get_cpu_sysdev(cpu);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001758 if (sys_dev) {
1759 switch (action) {
1760 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001761 case CPU_ONLINE_FROZEN:
Ashok Rajc32b6b82005-10-30 14:59:54 -08001762 cpufreq_add_dev(sys_dev);
1763 break;
1764 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001765 case CPU_DOWN_PREPARE_FROZEN:
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001766 if (unlikely(lock_policy_rwsem_write(cpu)))
1767 BUG();
1768
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001769 __cpufreq_remove_dev(sys_dev);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001770 break;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001771 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001772 case CPU_DOWN_FAILED_FROZEN:
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001773 cpufreq_add_dev(sys_dev);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001774 break;
1775 }
1776 }
1777 return NOTIFY_OK;
1778}
1779
Neal Buckendahl9c36f742010-06-22 22:02:44 -05001780static struct notifier_block __refdata cpufreq_cpu_notifier = {
Ashok Rajc32b6b82005-10-30 14:59:54 -08001781 .notifier_call = cpufreq_cpu_callback,
1782};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
1784/*********************************************************************
1785 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1786 *********************************************************************/
1787
1788/**
1789 * cpufreq_register_driver - register a CPU Frequency driver
1790 * @driver_data: A struct cpufreq_driver containing the values#
1791 * submitted by the CPU Frequency driver.
1792 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001793 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05001795 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 *
1797 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001798int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799{
1800 unsigned long flags;
1801 int ret;
1802
1803 if (!driver_data || !driver_data->verify || !driver_data->init ||
1804 ((!driver_data->setpolicy) && (!driver_data->target)))
1805 return -EINVAL;
1806
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001807 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809 if (driver_data->setpolicy)
1810 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1811
1812 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1813 if (cpufreq_driver) {
1814 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1815 return -EBUSY;
1816 }
1817 cpufreq_driver = driver_data;
1818 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1819
Mike Travis7a6aedf2008-03-25 15:06:53 -07001820 ret = sysdev_driver_register(&cpu_sysdev_class,
1821 &cpufreq_sysdev_driver);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001822 if (ret)
1823 goto err_null_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001825 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 int i;
1827 ret = -ENODEV;
1828
1829 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07001830 for (i = 0; i < nr_cpu_ids; i++)
1831 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07001833 break;
1834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
1836 /* if all ->init() calls failed, unregister */
1837 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001838 pr_debug("no CPU initialized for driver %s\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301839 driver_data->name);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001840 goto err_sysdev_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 }
1842 }
1843
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001844 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001845 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001847 return 0;
1848err_sysdev_unreg:
1849 sysdev_driver_unregister(&cpu_sysdev_class,
1850 &cpufreq_sysdev_driver);
1851err_null_driver:
1852 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1853 cpufreq_driver = NULL;
1854 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05001855 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856}
1857EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1858
1859
1860/**
1861 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1862 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001863 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 * the right to do so, i.e. if you have succeeded in initialising before!
1865 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1866 * currently not initialised.
1867 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001868int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869{
1870 unsigned long flags;
1871
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001872 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001875 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001878 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
1880 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1881 cpufreq_driver = NULL;
1882 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1883
1884 return 0;
1885}
1886EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001887
1888static int __init cpufreq_core_init(void)
1889{
1890 int cpu;
1891
1892 for_each_possible_cpu(cpu) {
Tejun Heof1625062009-10-29 22:34:13 +09001893 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001894 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
1895 }
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02001896
1897 cpufreq_global_kobject = kobject_create_and_add("cpufreq",
1898 &cpu_sysdev_class.kset.kobj);
1899 BUG_ON(!cpufreq_global_kobject);
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001900 register_syscore_ops(&cpufreq_syscore_ops);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02001901
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001902 return 0;
1903}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001904core_initcall(cpufreq_core_init);