blob: e9d654b42f3d017d7df14fc78e5ce1c336b9dcbb [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 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070044struct cpufreq_cpu_save_data {
45 char gov[CPUFREQ_NAME_LEN];
46 unsigned int max, min;
47};
48static DEFINE_PER_CPU(struct cpufreq_cpu_save_data, cpufreq_policy_save);
Thomas Renninger084f3492007-07-09 11:35:28 -070049#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static DEFINE_SPINLOCK(cpufreq_driver_lock);
51
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080052/*
53 * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
54 * all cpufreq/hotplug/workqueue/etc related lock issues.
55 *
56 * The rules for this semaphore:
57 * - Any routine that wants to read from the policy structure will
58 * do a down_read on this semaphore.
59 * - Any routine that will write to the policy structure and/or may take away
60 * the policy altogether (eg. CPU hotplug), will hold this lock in write
61 * mode before doing so.
62 *
63 * Additional rules:
64 * - All holders of the lock should check to make sure that the CPU they
65 * are concerned with are online after they get the lock.
66 * - Governor routines that can be called in cpufreq hotplug path should not
67 * take this sem as top level hotplug notifier handler takes this.
Mathieu Desnoyers395913d2009-06-08 13:17:31 -040068 * - Lock should not be held across
69 * __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080070 */
Tejun Heof1625062009-10-29 22:34:13 +090071static DEFINE_PER_CPU(int, cpufreq_policy_cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080072static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
73
74#define lock_policy_rwsem(mode, cpu) \
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070075int lock_policy_rwsem_##mode \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080076(int cpu) \
77{ \
Tejun Heof1625062009-10-29 22:34:13 +090078 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080079 BUG_ON(policy_cpu == -1); \
80 down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
81 if (unlikely(!cpu_online(cpu))) { \
82 up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
83 return -1; \
84 } \
85 \
86 return 0; \
87}
88
89lock_policy_rwsem(read, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080090
91lock_policy_rwsem(write, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080092
Amerigo Wang226528c2010-03-04 03:23:36 -050093static void unlock_policy_rwsem_read(int cpu)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080094{
Tejun Heof1625062009-10-29 22:34:13 +090095 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080096 BUG_ON(policy_cpu == -1);
97 up_read(&per_cpu(cpu_policy_rwsem, policy_cpu));
98}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080099
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700100void unlock_policy_rwsem_write(int cpu)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800101{
Tejun Heof1625062009-10-29 22:34:13 +0900102 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800103 BUG_ON(policy_cpu == -1);
104 up_write(&per_cpu(cpu_policy_rwsem, policy_cpu));
105}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800106
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/* internal prototypes */
Dave Jones29464f22009-01-18 01:37:11 -0500109static int __cpufreq_governor(struct cpufreq_policy *policy,
110 unsigned int event);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800111static unsigned int __cpufreq_get(unsigned int cpu);
David Howells65f27f32006-11-22 14:55:48 +0000112static void handle_update(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114/**
Dave Jones32ee8c32006-02-28 00:43:23 -0500115 * Two notifier lists: the "policy" list is involved in the
116 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 * "transition" list for kernel code that needs to handle
118 * changes to devices when the CPU clock speed changes.
119 * The mutex locks both lists.
120 */
Alan Sterne041c682006-03-27 01:16:30 -0800121static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700122static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200124static bool init_cpufreq_transition_notifier_list_called;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700125static int __init init_cpufreq_transition_notifier_list(void)
126{
127 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200128 init_cpufreq_transition_notifier_list_called = true;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700129 return 0;
130}
Linus Torvaldsb3438f82006-11-20 11:47:18 -0800131pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400133static int off __read_mostly;
134int cpufreq_disabled(void)
135{
136 return off;
137}
138void disable_cpufreq(void)
139{
140 off = 1;
141}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142static LIST_HEAD(cpufreq_governor_list);
Dave Jones29464f22009-01-18 01:37:11 -0500143static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Dave Jones7d5e3502006-02-02 17:03:42 -0500145struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 struct cpufreq_policy *data;
148 unsigned long flags;
149
Mike Travis7a6aedf2008-03-25 15:06:53 -0700150 if (cpu >= nr_cpu_ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 goto err_out;
152
153 /* get the cpufreq driver */
154 spin_lock_irqsave(&cpufreq_driver_lock, flags);
155
156 if (!cpufreq_driver)
157 goto err_out_unlock;
158
159 if (!try_module_get(cpufreq_driver->owner))
160 goto err_out_unlock;
161
162
163 /* get the CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -0700164 data = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 if (!data)
167 goto err_out_put_module;
168
169 if (!kobject_get(&data->kobj))
170 goto err_out_put_module;
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return data;
174
Dave Jones7d5e3502006-02-02 17:03:42 -0500175err_out_put_module:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 module_put(cpufreq_driver->owner);
Dave Jones7d5e3502006-02-02 17:03:42 -0500177err_out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones7d5e3502006-02-02 17:03:42 -0500179err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return NULL;
181}
182EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
183
Dave Jones7d5e3502006-02-02 17:03:42 -0500184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185void cpufreq_cpu_put(struct cpufreq_policy *data)
186{
187 kobject_put(&data->kobj);
188 module_put(cpufreq_driver->owner);
189}
190EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
191
192
193/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
195 *********************************************************************/
196
197/**
198 * adjust_jiffies - adjust the system "loops_per_jiffy"
199 *
200 * This function alters the system "loops_per_jiffy" for the clock
201 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500202 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 * per-CPU loops_per_jiffy value wherever possible.
204 */
205#ifndef CONFIG_SMP
206static unsigned long l_p_j_ref;
207static unsigned int l_p_j_ref_freq;
208
Arjan van de Ven858119e2006-01-14 13:20:43 -0800209static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 if (ci->flags & CPUFREQ_CONST_LOOPS)
212 return;
213
214 if (!l_p_j_ref_freq) {
215 l_p_j_ref = loops_per_jiffy;
216 l_p_j_ref_freq = ci->old;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200217 pr_debug("saving %lu as reference value for loops_per_jiffy; "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530218 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
Afzal Mohammedd08de0c12012-01-04 10:52:46 +0530220 if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700221 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530222 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
223 ci->new);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200224 pr_debug("scaling loops_per_jiffy to %lu "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530225 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
227}
228#else
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530229static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
230{
231 return;
232}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233#endif
234
235
236/**
Dave Jonese4472cb2006-01-31 15:53:55 -0800237 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
238 * on frequency transition.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 *
Dave Jonese4472cb2006-01-31 15:53:55 -0800240 * This function calls the transition notifiers and the "adjust_jiffies"
241 * function. It is called twice on all CPU frequency changes that have
Dave Jones32ee8c32006-02-28 00:43:23 -0500242 * external effects.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 */
244void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
245{
Dave Jonese4472cb2006-01-31 15:53:55 -0800246 struct cpufreq_policy *policy;
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 BUG_ON(irqs_disabled());
249
250 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200251 pr_debug("notification %u of frequency transition to %u kHz\n",
Dave Jonese4472cb2006-01-31 15:53:55 -0800252 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Mike Travis7a6aedf2008-03-25 15:06:53 -0700254 policy = per_cpu(cpufreq_cpu_data, freqs->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500258 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800259 * which is not equal to what the cpufreq core thinks is
260 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 */
262 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800263 if ((policy) && (policy->cpu == freqs->cpu) &&
264 (policy->cur) && (policy->cur != freqs->old)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200265 pr_debug("Warning: CPU frequency is"
Dave Jonese4472cb2006-01-31 15:53:55 -0800266 " %u, cpufreq assumed %u kHz.\n",
267 freqs->old, policy->cur);
268 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700271 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800272 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
274 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 case CPUFREQ_POSTCHANGE:
277 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200278 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
Thomas Renninger6f4f2722010-04-20 13:17:36 +0200279 (unsigned long)freqs->cpu);
280 trace_power_frequency(POWER_PSTATE, freqs->new, freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100281 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700282 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800283 CPUFREQ_POSTCHANGE, freqs);
Amar Singhal99055d52012-02-23 13:54:46 -0800284 if (likely(policy) && likely(policy->cpu == freqs->cpu)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800285 policy->cur = freqs->new;
Amar Singhal99055d52012-02-23 13:54:46 -0800286 sysfs_notify(&policy->kobj, NULL, "scaling_cur_freq");
287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 break;
289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800292/**
293 * cpufreq_notify_utilization - notify CPU userspace about CPU utilization
294 * change
295 *
296 * This function is called everytime the CPU load is evaluated by the
297 * ondemand governor. It notifies userspace of cpu load changes via sysfs.
298 */
299void cpufreq_notify_utilization(struct cpufreq_policy *policy,
300 unsigned int util)
301{
302 if (policy)
303 policy->util = util;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800305 if (policy->util >= MIN_CPU_UTIL_NOTIFY)
306 sysfs_notify(&policy->kobj, NULL, "cpu_utilization");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800308}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310/*********************************************************************
311 * SYSFS INTERFACE *
312 *********************************************************************/
313
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700314static struct cpufreq_governor *__find_governor(const char *str_governor)
315{
316 struct cpufreq_governor *t;
317
318 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
Dave Jones29464f22009-01-18 01:37:11 -0500319 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700320 return t;
321
322 return NULL;
323}
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325/**
326 * cpufreq_parse_governor - parse a governor string
327 */
Dave Jones905d77c2008-03-05 14:28:32 -0500328static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 struct cpufreq_governor **governor)
330{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700331 int err = -EINVAL;
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700334 goto out;
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (cpufreq_driver->setpolicy) {
337 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
338 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700339 err = 0;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530340 } else if (!strnicmp(str_governor, "powersave",
341 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700343 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700345 } else if (cpufreq_driver->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700347
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800348 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700349
350 t = __find_governor(str_governor);
351
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700352 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700353 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700354
Kees Cook1a8e1462011-05-04 08:38:56 -0700355 mutex_unlock(&cpufreq_governor_mutex);
356 ret = request_module("cpufreq_%s", str_governor);
357 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700358
Kees Cook1a8e1462011-05-04 08:38:56 -0700359 if (ret == 0)
360 t = __find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700361 }
362
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700363 if (t != NULL) {
364 *governor = t;
365 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700367
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800368 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
Dave Jones29464f22009-01-18 01:37:11 -0500370out:
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700371 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530376 * cpufreq_per_cpu_attr_read() / show_##file_name() -
377 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 *
379 * Write out information from cpufreq_driver->policy[cpu]; object must be
380 * "unsigned int".
381 */
382
Dave Jones32ee8c32006-02-28 00:43:23 -0500383#define show_one(file_name, object) \
384static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500385(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500386{ \
Dave Jones29464f22009-01-18 01:37:11 -0500387 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
390show_one(cpuinfo_min_freq, cpuinfo.min_freq);
391show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100392show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393show_one(scaling_min_freq, min);
394show_one(scaling_max_freq, max);
395show_one(scaling_cur_freq, cur);
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800396show_one(cpu_utilization, util);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530398static int __cpufreq_set_policy(struct cpufreq_policy *data,
399 struct cpufreq_policy *policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401/**
402 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
403 */
404#define store_one(file_name, object) \
405static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500406(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{ \
408 unsigned int ret = -EINVAL; \
409 struct cpufreq_policy new_policy; \
410 \
411 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
412 if (ret) \
413 return -EINVAL; \
414 \
Dave Jones29464f22009-01-18 01:37:11 -0500415 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (ret != 1) \
417 return -EINVAL; \
418 \
Thomas Renninger7970e082006-04-13 15:14:04 +0200419 ret = __cpufreq_set_policy(policy, &new_policy); \
420 policy->user_policy.object = policy->object; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 \
422 return ret ? ret : count; \
423}
424
Dave Jones29464f22009-01-18 01:37:11 -0500425store_one(scaling_min_freq, min);
426store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428/**
429 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
430 */
Dave Jones905d77c2008-03-05 14:28:32 -0500431static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
432 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800434 unsigned int cur_freq = __cpufreq_get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (!cur_freq)
436 return sprintf(buf, "<unknown>");
437 return sprintf(buf, "%u\n", cur_freq);
438}
439
440
441/**
442 * show_scaling_governor - show the current policy for the specified CPU
443 */
Dave Jones905d77c2008-03-05 14:28:32 -0500444static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Dave Jones29464f22009-01-18 01:37:11 -0500446 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return sprintf(buf, "powersave\n");
448 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
449 return sprintf(buf, "performance\n");
450 else if (policy->governor)
Dave Jones29464f22009-01-18 01:37:11 -0500451 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n",
452 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return -EINVAL;
454}
455
456
457/**
458 * store_scaling_governor - store policy for the specified CPU
459 */
Dave Jones905d77c2008-03-05 14:28:32 -0500460static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
461 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 unsigned int ret = -EINVAL;
464 char str_governor[16];
465 struct cpufreq_policy new_policy;
466
467 ret = cpufreq_get_policy(&new_policy, policy->cpu);
468 if (ret)
469 return ret;
470
Dave Jones29464f22009-01-18 01:37:11 -0500471 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (ret != 1)
473 return -EINVAL;
474
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530475 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
476 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return -EINVAL;
478
Thomas Renninger7970e082006-04-13 15:14:04 +0200479 /* Do not use cpufreq_set_policy here or the user_policy.max
480 will be wrongly overridden */
Thomas Renninger7970e082006-04-13 15:14:04 +0200481 ret = __cpufreq_set_policy(policy, &new_policy);
482
483 policy->user_policy.policy = policy->policy;
484 policy->user_policy.governor = policy->governor;
Thomas Renninger7970e082006-04-13 15:14:04 +0200485
Amar Singhal37d0b022012-05-25 14:40:05 -0700486 sysfs_notify(&policy->kobj, NULL, "scaling_governor");
487
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530488 if (ret)
489 return ret;
490 else
491 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
494/**
495 * show_scaling_driver - show the cpufreq driver currently loaded
496 */
Dave Jones905d77c2008-03-05 14:28:32 -0500497static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
500}
501
502/**
503 * show_scaling_available_governors - show the available CPUfreq governors
504 */
Dave Jones905d77c2008-03-05 14:28:32 -0500505static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
506 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
508 ssize_t i = 0;
509 struct cpufreq_governor *t;
510
511 if (!cpufreq_driver->target) {
512 i += sprintf(buf, "performance powersave");
513 goto out;
514 }
515
516 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
Dave Jones29464f22009-01-18 01:37:11 -0500517 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
518 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 goto out;
520 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
521 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500522out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 i += sprintf(&buf[i], "\n");
524 return i;
525}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700526
Rusty Russell835481d2009-01-04 05:18:06 -0800527static ssize_t show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 ssize_t i = 0;
530 unsigned int cpu;
531
Rusty Russell835481d2009-01-04 05:18:06 -0800532 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (i)
534 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
535 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
536 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500537 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539 i += sprintf(&buf[i], "\n");
540 return i;
541}
542
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700543/**
544 * show_related_cpus - show the CPUs affected by each transition even if
545 * hw coordination is in use
546 */
547static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
548{
Rusty Russell835481d2009-01-04 05:18:06 -0800549 if (cpumask_empty(policy->related_cpus))
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700550 return show_cpus(policy->cpus, buf);
551 return show_cpus(policy->related_cpus, buf);
552}
553
554/**
555 * show_affected_cpus - show the CPUs affected by each transition
556 */
557static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
558{
559 return show_cpus(policy->cpus, buf);
560}
561
Venki Pallipadi9e769882007-10-26 10:18:21 -0700562static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500563 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700564{
565 unsigned int freq = 0;
566 unsigned int ret;
567
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700568 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700569 return -EINVAL;
570
571 ret = sscanf(buf, "%u", &freq);
572 if (ret != 1)
573 return -EINVAL;
574
575 policy->governor->store_setspeed(policy, freq);
576
577 return count;
578}
579
580static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
581{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700582 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700583 return sprintf(buf, "<unsupported>\n");
584
585 return policy->governor->show_setspeed(policy, buf);
586}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Thomas Renningere2f74f32009-11-19 12:31:01 +0100588/**
589 * show_scaling_driver - show the current cpufreq HW/BIOS limitation
590 */
591static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
592{
593 unsigned int limit;
594 int ret;
595 if (cpufreq_driver->bios_limit) {
596 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
597 if (!ret)
598 return sprintf(buf, "%u\n", limit);
599 }
600 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
601}
602
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200603cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
604cpufreq_freq_attr_ro(cpuinfo_min_freq);
605cpufreq_freq_attr_ro(cpuinfo_max_freq);
606cpufreq_freq_attr_ro(cpuinfo_transition_latency);
607cpufreq_freq_attr_ro(scaling_available_governors);
608cpufreq_freq_attr_ro(scaling_driver);
609cpufreq_freq_attr_ro(scaling_cur_freq);
610cpufreq_freq_attr_ro(bios_limit);
611cpufreq_freq_attr_ro(related_cpus);
612cpufreq_freq_attr_ro(affected_cpus);
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800613cpufreq_freq_attr_ro(cpu_utilization);
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200614cpufreq_freq_attr_rw(scaling_min_freq);
615cpufreq_freq_attr_rw(scaling_max_freq);
616cpufreq_freq_attr_rw(scaling_governor);
617cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Dave Jones905d77c2008-03-05 14:28:32 -0500619static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 &cpuinfo_min_freq.attr,
621 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100622 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 &scaling_min_freq.attr,
624 &scaling_max_freq.attr,
625 &affected_cpus.attr,
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800626 &cpu_utilization.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700627 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 &scaling_governor.attr,
629 &scaling_driver.attr,
630 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700631 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 NULL
633};
634
Thomas Renninger8aa84ad2009-07-24 15:25:05 +0200635struct kobject *cpufreq_global_kobject;
636EXPORT_SYMBOL(cpufreq_global_kobject);
637
Dave Jones29464f22009-01-18 01:37:11 -0500638#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
639#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Dave Jones29464f22009-01-18 01:37:11 -0500641static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Dave Jones905d77c2008-03-05 14:28:32 -0500643 struct cpufreq_policy *policy = to_policy(kobj);
644 struct freq_attr *fattr = to_attr(attr);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500645 ssize_t ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 policy = cpufreq_cpu_get(policy->cpu);
647 if (!policy)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500648 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800649
650 if (lock_policy_rwsem_read(policy->cpu) < 0)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500651 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800652
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530653 if (fattr->show)
654 ret = fattr->show(policy, buf);
655 else
656 ret = -EIO;
657
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800658 unlock_policy_rwsem_read(policy->cpu);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500659fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 cpufreq_cpu_put(policy);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500661no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return ret;
663}
664
Dave Jones905d77c2008-03-05 14:28:32 -0500665static ssize_t store(struct kobject *kobj, struct attribute *attr,
666 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Dave Jones905d77c2008-03-05 14:28:32 -0500668 struct cpufreq_policy *policy = to_policy(kobj);
669 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500670 ssize_t ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 policy = cpufreq_cpu_get(policy->cpu);
672 if (!policy)
Dave Jonesa07530b2008-03-05 14:22:25 -0500673 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800674
675 if (lock_policy_rwsem_write(policy->cpu) < 0)
Dave Jonesa07530b2008-03-05 14:22:25 -0500676 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800677
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530678 if (fattr->store)
679 ret = fattr->store(policy, buf, count);
680 else
681 ret = -EIO;
682
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800683 unlock_policy_rwsem_write(policy->cpu);
Dave Jonesa07530b2008-03-05 14:22:25 -0500684fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 cpufreq_cpu_put(policy);
Dave Jonesa07530b2008-03-05 14:22:25 -0500686no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return ret;
688}
689
Dave Jones905d77c2008-03-05 14:28:32 -0500690static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Dave Jones905d77c2008-03-05 14:28:32 -0500692 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200693 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 complete(&policy->kobj_unregister);
695}
696
Emese Revfy52cf25d2010-01-19 02:58:23 +0100697static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 .show = show,
699 .store = store,
700};
701
702static struct kobj_type ktype_cpufreq = {
703 .sysfs_ops = &sysfs_ops,
704 .default_attrs = default_attrs,
705 .release = cpufreq_sysfs_release,
706};
707
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200708/*
709 * Returns:
710 * Negative: Failure
711 * 0: Success
712 * Positive: When we have a managed CPU and the sysfs got symlinked
713 */
Alex Chiangcf3289d2009-11-17 20:27:08 -0700714static int cpufreq_add_dev_policy(unsigned int cpu,
715 struct cpufreq_policy *policy,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800716 struct device *dev)
Dave Jonesecf7e462009-07-08 18:48:47 -0400717{
718 int ret = 0;
719#ifdef CONFIG_SMP
720 unsigned long flags;
721 unsigned int j;
Dave Jonesecf7e462009-07-08 18:48:47 -0400722#ifdef CONFIG_HOTPLUG_CPU
Dmitry Monakhove77b89f2009-10-05 00:38:55 +0400723 struct cpufreq_governor *gov;
724
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700725 gov = __find_governor(per_cpu(cpufreq_policy_save, cpu).gov);
Dmitry Monakhove77b89f2009-10-05 00:38:55 +0400726 if (gov) {
727 policy->governor = gov;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200728 pr_debug("Restoring governor %s for cpu %d\n",
Dave Jonesecf7e462009-07-08 18:48:47 -0400729 policy->governor->name, cpu);
730 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700731 if (per_cpu(cpufreq_policy_save, cpu).min) {
732 policy->min = per_cpu(cpufreq_policy_save, cpu).min;
733 policy->user_policy.min = policy->min;
734 }
735 if (per_cpu(cpufreq_policy_save, cpu).max) {
736 policy->max = per_cpu(cpufreq_policy_save, cpu).max;
737 policy->user_policy.max = policy->max;
738 }
739 pr_debug("Restoring CPU%d min %d and max %d\n",
740 cpu, policy->min, policy->max);
Dave Jonesecf7e462009-07-08 18:48:47 -0400741#endif
742
743 for_each_cpu(j, policy->cpus) {
744 struct cpufreq_policy *managed_policy;
745
746 if (cpu == j)
747 continue;
748
749 /* Check for existing affected CPUs.
750 * They may not be aware of it due to CPU Hotplug.
751 * cpufreq_cpu_put is called when the device is removed
752 * in __cpufreq_remove_dev()
753 */
754 managed_policy = cpufreq_cpu_get(j);
755 if (unlikely(managed_policy)) {
756
757 /* Set proper policy_cpu */
758 unlock_policy_rwsem_write(cpu);
Tejun Heof1625062009-10-29 22:34:13 +0900759 per_cpu(cpufreq_policy_cpu, cpu) = managed_policy->cpu;
Dave Jonesecf7e462009-07-08 18:48:47 -0400760
761 if (lock_policy_rwsem_write(cpu) < 0) {
762 /* Should not go through policy unlock path */
763 if (cpufreq_driver->exit)
764 cpufreq_driver->exit(policy);
765 cpufreq_cpu_put(managed_policy);
766 return -EBUSY;
767 }
768
769 spin_lock_irqsave(&cpufreq_driver_lock, flags);
770 cpumask_copy(managed_policy->cpus, policy->cpus);
771 per_cpu(cpufreq_cpu_data, cpu) = managed_policy;
772 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
773
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200774 pr_debug("CPU already managed, adding link\n");
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800775 ret = sysfs_create_link(&dev->kobj,
Dave Jonesecf7e462009-07-08 18:48:47 -0400776 &managed_policy->kobj,
777 "cpufreq");
778 if (ret)
779 cpufreq_cpu_put(managed_policy);
780 /*
781 * Success. We only needed to be added to the mask.
782 * Call driver->exit() because only the cpu parent of
783 * the kobj needed to call init().
784 */
785 if (cpufreq_driver->exit)
786 cpufreq_driver->exit(policy);
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200787
788 if (!ret)
789 return 1;
790 else
791 return ret;
Dave Jonesecf7e462009-07-08 18:48:47 -0400792 }
793 }
794#endif
795 return ret;
796}
797
798
Dave Jones19d6f7e2009-07-08 17:35:39 -0400799/* symlink affected CPUs */
Alex Chiangcf3289d2009-11-17 20:27:08 -0700800static int cpufreq_add_dev_symlink(unsigned int cpu,
801 struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400802{
803 unsigned int j;
804 int ret = 0;
805
806 for_each_cpu(j, policy->cpus) {
807 struct cpufreq_policy *managed_policy;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800808 struct device *cpu_dev;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400809
810 if (j == cpu)
811 continue;
812 if (!cpu_online(j))
813 continue;
814
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200815 pr_debug("CPU %u already managed, adding link\n", j);
Dave Jones19d6f7e2009-07-08 17:35:39 -0400816 managed_policy = cpufreq_cpu_get(cpu);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800817 cpu_dev = get_cpu_device(j);
818 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Dave Jones19d6f7e2009-07-08 17:35:39 -0400819 "cpufreq");
820 if (ret) {
821 cpufreq_cpu_put(managed_policy);
822 return ret;
823 }
824 }
825 return ret;
826}
827
Alex Chiangcf3289d2009-11-17 20:27:08 -0700828static int cpufreq_add_dev_interface(unsigned int cpu,
829 struct cpufreq_policy *policy,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800830 struct device *dev)
Dave Jones909a6942009-07-08 18:05:42 -0400831{
Dave Jonesecf7e462009-07-08 18:48:47 -0400832 struct cpufreq_policy new_policy;
Dave Jones909a6942009-07-08 18:05:42 -0400833 struct freq_attr **drv_attr;
834 unsigned long flags;
835 int ret = 0;
836 unsigned int j;
837
838 /* prepare interface data */
839 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800840 &dev->kobj, "cpufreq");
Dave Jones909a6942009-07-08 18:05:42 -0400841 if (ret)
842 return ret;
843
844 /* set up files for this cpu device */
845 drv_attr = cpufreq_driver->attr;
846 while ((drv_attr) && (*drv_attr)) {
847 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
848 if (ret)
849 goto err_out_kobj_put;
850 drv_attr++;
851 }
852 if (cpufreq_driver->get) {
853 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
854 if (ret)
855 goto err_out_kobj_put;
856 }
857 if (cpufreq_driver->target) {
858 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
859 if (ret)
860 goto err_out_kobj_put;
861 }
Thomas Renningere2f74f32009-11-19 12:31:01 +0100862 if (cpufreq_driver->bios_limit) {
863 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
864 if (ret)
865 goto err_out_kobj_put;
866 }
Dave Jones909a6942009-07-08 18:05:42 -0400867
868 spin_lock_irqsave(&cpufreq_driver_lock, flags);
869 for_each_cpu(j, policy->cpus) {
Julia Lawallbec037a2010-08-05 22:23:00 +0200870 if (!cpu_online(j))
871 continue;
Dave Jones909a6942009-07-08 18:05:42 -0400872 per_cpu(cpufreq_cpu_data, j) = policy;
Tejun Heof1625062009-10-29 22:34:13 +0900873 per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
Dave Jones909a6942009-07-08 18:05:42 -0400874 }
875 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
876
877 ret = cpufreq_add_dev_symlink(cpu, policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400878 if (ret)
879 goto err_out_kobj_put;
880
881 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
882 /* assure that the starting sequence is run in __cpufreq_set_policy */
883 policy->governor = NULL;
884
885 /* set default policy */
886 ret = __cpufreq_set_policy(policy, &new_policy);
887 policy->user_policy.policy = policy->policy;
888 policy->user_policy.governor = policy->governor;
889
890 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200891 pr_debug("setting policy failed\n");
Dave Jonesecf7e462009-07-08 18:48:47 -0400892 if (cpufreq_driver->exit)
893 cpufreq_driver->exit(policy);
894 }
Dave Jones909a6942009-07-08 18:05:42 -0400895 return ret;
896
897err_out_kobj_put:
898 kobject_put(&policy->kobj);
899 wait_for_completion(&policy->kobj_unregister);
900 return ret;
901}
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904/**
905 * cpufreq_add_dev - add a CPU device
906 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500907 * Adds the cpufreq interface for a CPU device.
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400908 *
909 * The Oracle says: try running cpufreq registration/unregistration concurrently
910 * with with cpu hotplugging and all hell will break loose. Tried to clean this
911 * mess up, but more thorough testing is needed. - Mathieu
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800913static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800915 unsigned int cpu = dev->id;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500916 int ret = 0, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 unsigned long flags;
919 unsigned int j;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500920#ifdef CONFIG_HOTPLUG_CPU
921 int sibling;
922#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Ashok Rajc32b6b82005-10-30 14:59:54 -0800924 if (cpu_is_offline(cpu))
925 return 0;
926
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200927 pr_debug("adding CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929#ifdef CONFIG_SMP
930 /* check whether a different CPU already registered this
931 * CPU because it is in the same boat. */
932 policy = cpufreq_cpu_get(cpu);
933 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -0500934 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return 0;
936 }
937#endif
938
939 if (!try_module_get(cpufreq_driver->owner)) {
940 ret = -EINVAL;
941 goto module_out;
942 }
943
Dave Jones059019a2009-07-08 16:30:03 -0400944 ret = -ENOMEM;
Dave Jonese98df502005-10-20 15:17:43 -0700945 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
Dave Jones059019a2009-07-08 16:30:03 -0400946 if (!policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 goto nomem_out;
Dave Jones059019a2009-07-08 16:30:03 -0400948
949 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400950 goto err_free_policy;
Dave Jones059019a2009-07-08 16:30:03 -0400951
952 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400953 goto err_free_cpumask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 policy->cpu = cpu;
Rusty Russell835481d2009-01-04 05:18:06 -0800956 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800958 /* Initially set CPU itself as the policy_cpu */
Tejun Heof1625062009-10-29 22:34:13 +0900959 per_cpu(cpufreq_policy_cpu, cpu) = cpu;
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400960 ret = (lock_policy_rwsem_write(cpu) < 0);
961 WARN_ON(ret);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 init_completion(&policy->kobj_unregister);
David Howells65f27f32006-11-22 14:55:48 +0000964 INIT_WORK(&policy->update, handle_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Thomas Renninger8122c6c2007-10-02 13:28:09 -0700966 /* Set governor before ->init, so that driver could check it */
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500967#ifdef CONFIG_HOTPLUG_CPU
968 for_each_online_cpu(sibling) {
969 struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
970 if (cp && cp->governor &&
971 (cpumask_test_cpu(cpu, cp->related_cpus))) {
972 policy->governor = cp->governor;
973 found = 1;
974 break;
975 }
976 }
977#endif
978 if (!found)
979 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 /* call driver. From then on the cpufreq must be able
981 * to accept all calls to ->verify and ->setpolicy for this CPU
982 */
983 ret = cpufreq_driver->init(policy);
984 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200985 pr_debug("initialization failed\n");
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400986 goto err_unlock_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
Mike Chan187d9f42008-12-04 12:19:17 -0800988 policy->user_policy.min = policy->min;
989 policy->user_policy.max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Thomas Renningera1531ac2008-07-29 22:32:58 -0700991 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
992 CPUFREQ_START, policy);
993
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800994 ret = cpufreq_add_dev_policy(cpu, policy, dev);
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200995 if (ret) {
996 if (ret > 0)
997 /* This is a managed cpu, symlink created,
998 exit with 0 */
999 ret = 0;
Dave Jonesecf7e462009-07-08 18:48:47 -04001000 goto err_unlock_policy;
Thomas Renninger4bfa0422009-07-24 15:25:03 +02001001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001003 ret = cpufreq_add_dev_interface(cpu, policy, dev);
Dave Jones19d6f7e2009-07-08 17:35:39 -04001004 if (ret)
1005 goto err_out_unregister;
Dave Jones8ff69732006-03-05 03:37:23 -05001006
Lothar Waßmanndca02612008-05-29 17:54:52 +02001007 unlock_policy_rwsem_write(cpu);
1008
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001009 kobject_uevent(&policy->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 module_put(cpufreq_driver->owner);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001011 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return 0;
1014
1015
1016err_out_unregister:
1017 spin_lock_irqsave(&cpufreq_driver_lock, flags);
Rusty Russell835481d2009-01-04 05:18:06 -08001018 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -07001019 per_cpu(cpufreq_cpu_data, j) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1021
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -08001022 kobject_put(&policy->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 wait_for_completion(&policy->kobj_unregister);
1024
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001025err_unlock_policy:
Dave Jones45709112008-03-05 14:07:34 -05001026 unlock_policy_rwsem_write(cpu);
Xiaotian Fengcad70a62010-07-20 20:11:02 +08001027 free_cpumask_var(policy->related_cpus);
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001028err_free_cpumask:
1029 free_cpumask_var(policy->cpus);
1030err_free_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 kfree(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032nomem_out:
1033 module_put(cpufreq_driver->owner);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001034module_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 return ret;
1036}
1037
1038
1039/**
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001040 * __cpufreq_remove_dev - remove a CPU device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 *
1042 * Removes the cpufreq interface for a CPU device.
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001043 * Caller should already have policy_rwsem in write mode for this CPU.
1044 * This routine frees the rwsem before returning.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001046static int __cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001048 unsigned int cpu = dev->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 unsigned long flags;
1050 struct cpufreq_policy *data;
Amerigo Wang499bca92010-03-04 03:23:46 -05001051 struct kobject *kobj;
1052 struct completion *cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053#ifdef CONFIG_SMP
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001054 struct device *cpu_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 unsigned int j;
1056#endif
1057
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001058 pr_debug("unregistering CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 spin_lock_irqsave(&cpufreq_driver_lock, flags);
Mike Travis7a6aedf2008-03-25 15:06:53 -07001061 data = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 if (!data) {
1064 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001065 unlock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 return -EINVAL;
1067 }
Mike Travis7a6aedf2008-03-25 15:06:53 -07001068 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070
1071#ifdef CONFIG_SMP
1072 /* if this isn't the CPU which is the parent of the kobj, we
Dave Jones32ee8c32006-02-28 00:43:23 -05001073 * only need to unlink, put and exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 */
1075 if (unlikely(cpu != data->cpu)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001076 pr_debug("removing link\n");
Rusty Russell835481d2009-01-04 05:18:06 -08001077 cpumask_clear_cpu(cpu, data->cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001079 kobj = &dev->kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 cpufreq_cpu_put(data);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001081 unlock_policy_rwsem_write(cpu);
Amerigo Wang499bca92010-03-04 03:23:46 -05001082 sysfs_remove_link(kobj, "cpufreq");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return 0;
1084 }
1085#endif
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087#ifdef CONFIG_SMP
Thomas Renninger084f3492007-07-09 11:35:28 -07001088
1089#ifdef CONFIG_HOTPLUG_CPU
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001090 strncpy(per_cpu(cpufreq_policy_save, cpu).gov, data->governor->name,
Dmitry Monakhove77b89f2009-10-05 00:38:55 +04001091 CPUFREQ_NAME_LEN);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001092 per_cpu(cpufreq_policy_save, cpu).min = data->min;
1093 per_cpu(cpufreq_policy_save, cpu).max = data->max;
1094 pr_debug("Saving CPU%d policy min %d and max %d\n",
1095 cpu, data->min, data->max);
Thomas Renninger084f3492007-07-09 11:35:28 -07001096#endif
1097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 /* if we have other CPUs still registered, we need to unlink them,
1099 * or else wait_for_completion below will lock up. Clean the
Mike Travis7a6aedf2008-03-25 15:06:53 -07001100 * per_cpu(cpufreq_cpu_data) while holding the lock, and remove
1101 * the sysfs links afterwards.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 */
Rusty Russell835481d2009-01-04 05:18:06 -08001103 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1104 for_each_cpu(j, data->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 if (j == cpu)
1106 continue;
Mike Travis7a6aedf2008-03-25 15:06:53 -07001107 per_cpu(cpufreq_cpu_data, j) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 }
1109 }
1110
1111 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1112
Rusty Russell835481d2009-01-04 05:18:06 -08001113 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1114 for_each_cpu(j, data->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (j == cpu)
1116 continue;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001117 pr_debug("removing link for cpu %u\n", j);
Thomas Renninger084f3492007-07-09 11:35:28 -07001118#ifdef CONFIG_HOTPLUG_CPU
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001119 strncpy(per_cpu(cpufreq_policy_save, j).gov,
Dmitry Monakhove77b89f2009-10-05 00:38:55 +04001120 data->governor->name, CPUFREQ_NAME_LEN);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001121 per_cpu(cpufreq_policy_save, j).min = data->min;
1122 per_cpu(cpufreq_policy_save, j).max = data->max;
1123 pr_debug("Saving CPU%d policy min %d and max %d\n",
1124 j, data->min, data->max);
Thomas Renninger084f3492007-07-09 11:35:28 -07001125#endif
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001126 cpu_dev = get_cpu_device(j);
1127 kobj = &cpu_dev->kobj;
Amerigo Wang499bca92010-03-04 03:23:46 -05001128 unlock_policy_rwsem_write(cpu);
1129 sysfs_remove_link(kobj, "cpufreq");
1130 lock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 cpufreq_cpu_put(data);
1132 }
1133 }
1134#else
1135 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1136#endif
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (cpufreq_driver->target)
1139 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001140
Amerigo Wang499bca92010-03-04 03:23:46 -05001141 kobj = &data->kobj;
1142 cmp = &data->kobj_unregister;
1143 unlock_policy_rwsem_write(cpu);
1144 kobject_put(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 /* we need to make sure that the underlying kobj is actually
Dave Jones32ee8c32006-02-28 00:43:23 -05001147 * not referenced anymore by anybody before we proceed with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 * unloading.
1149 */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001150 pr_debug("waiting for dropping of refcount\n");
Amerigo Wang499bca92010-03-04 03:23:46 -05001151 wait_for_completion(cmp);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001152 pr_debug("wait complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Amerigo Wang499bca92010-03-04 03:23:46 -05001154 lock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (cpufreq_driver->exit)
1156 cpufreq_driver->exit(data);
venkatesh.pallipadi@intel.com7d26e2d2009-07-02 17:08:30 -07001157 unlock_policy_rwsem_write(cpu);
1158
Jacob Shin27ecddc2011-04-27 13:32:11 -05001159#ifdef CONFIG_HOTPLUG_CPU
1160 /* when the CPU which is the parent of the kobj is hotplugged
1161 * offline, check for siblings, and create cpufreq sysfs interface
1162 * and symlinks
1163 */
1164 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1165 /* first sibling now owns the new sysfs dir */
1166 cpumask_clear_cpu(cpu, data->cpus);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001167 cpufreq_add_dev(get_cpu_device(cpumask_first(data->cpus)), NULL);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001168
1169 /* finally remove our own symlink */
1170 lock_policy_rwsem_write(cpu);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001171 __cpufreq_remove_dev(dev, sif);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001172 }
1173#endif
1174
Rusty Russell835481d2009-01-04 05:18:06 -08001175 free_cpumask_var(data->related_cpus);
1176 free_cpumask_var(data->cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 kfree(data);
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 return 0;
1180}
1181
1182
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001183static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001184{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001185 unsigned int cpu = dev->id;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001186 int retval;
Venki Pallipadiec282972007-03-26 12:03:19 -07001187
1188 if (cpu_is_offline(cpu))
1189 return 0;
1190
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001191 if (unlikely(lock_policy_rwsem_write(cpu)))
1192 BUG();
1193
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001194 retval = __cpufreq_remove_dev(dev, sif);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001195 return retval;
1196}
1197
1198
David Howells65f27f32006-11-22 14:55:48 +00001199static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
David Howells65f27f32006-11-22 14:55:48 +00001201 struct cpufreq_policy *policy =
1202 container_of(work, struct cpufreq_policy, update);
1203 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001204 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 cpufreq_update_policy(cpu);
1206}
1207
1208/**
1209 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
1210 * @cpu: cpu number
1211 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1212 * @new_freq: CPU frequency the CPU actually runs at
1213 *
Dave Jones29464f22009-01-18 01:37:11 -05001214 * We adjust to current frequency first, and need to clean up later.
1215 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301217static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1218 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
1220 struct cpufreq_freqs freqs;
1221
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001222 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1224
1225 freqs.cpu = cpu;
1226 freqs.old = old_freq;
1227 freqs.new = new_freq;
1228 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
1229 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
1230}
1231
1232
Dave Jones32ee8c32006-02-28 00:43:23 -05001233/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301234 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001235 * @cpu: CPU number
1236 *
1237 * This is the last known freq, without actually getting it from the driver.
1238 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1239 */
1240unsigned int cpufreq_quick_get(unsigned int cpu)
1241{
1242 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301243 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001244
1245 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301246 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001247 cpufreq_cpu_put(policy);
1248 }
1249
Dave Jones4d34a672008-02-07 16:33:49 -05001250 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001251}
1252EXPORT_SYMBOL(cpufreq_quick_get);
1253
Jesse Barnes3d737102011-06-28 10:59:12 -07001254/**
1255 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1256 * @cpu: CPU number
1257 *
1258 * Just return the max possible frequency for a given CPU.
1259 */
1260unsigned int cpufreq_quick_get_max(unsigned int cpu)
1261{
1262 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1263 unsigned int ret_freq = 0;
1264
1265 if (policy) {
1266 ret_freq = policy->max;
1267 cpufreq_cpu_put(policy);
1268 }
1269
1270 return ret_freq;
1271}
1272EXPORT_SYMBOL(cpufreq_quick_get_max);
1273
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001274
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001275static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001277 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301278 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001281 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301283 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301285 if (ret_freq && policy->cur &&
1286 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1287 /* verify no discrepancy between actual and
1288 saved value exists */
1289 if (unlikely(ret_freq != policy->cur)) {
1290 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 schedule_work(&policy->update);
1292 }
1293 }
1294
Dave Jones4d34a672008-02-07 16:33:49 -05001295 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001296}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001298/**
1299 * cpufreq_get - get the current CPU frequency (in kHz)
1300 * @cpu: CPU number
1301 *
1302 * Get the CPU current (static) CPU frequency
1303 */
1304unsigned int cpufreq_get(unsigned int cpu)
1305{
1306 unsigned int ret_freq = 0;
1307 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1308
1309 if (!policy)
1310 goto out;
1311
1312 if (unlikely(lock_policy_rwsem_read(cpu)))
1313 goto out_policy;
1314
1315 ret_freq = __cpufreq_get(cpu);
1316
1317 unlock_policy_rwsem_read(cpu);
1318
1319out_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 cpufreq_cpu_put(policy);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001321out:
Dave Jones4d34a672008-02-07 16:33:49 -05001322 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
1324EXPORT_SYMBOL(cpufreq_get);
1325
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001326static struct subsys_interface cpufreq_interface = {
1327 .name = "cpufreq",
1328 .subsys = &cpu_subsys,
1329 .add_dev = cpufreq_add_dev,
1330 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001331};
1332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001335 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1336 *
1337 * This function is only executed for the boot processor. The other CPUs
1338 * have been put offline by means of CPU hotplug.
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001339 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001340static int cpufreq_bp_suspend(void)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001341{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301342 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001343
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001344 int cpu = smp_processor_id();
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001345 struct cpufreq_policy *cpu_policy;
1346
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001347 pr_debug("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001348
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001349 /* If there's no policy for the boot CPU, we have nothing to do. */
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001350 cpu_policy = cpufreq_cpu_get(cpu);
1351 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001352 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001353
1354 if (cpufreq_driver->suspend) {
Rafael J. Wysocki7ca64e22011-03-10 21:13:05 +01001355 ret = cpufreq_driver->suspend(cpu_policy);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001356 if (ret)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001357 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1358 "step on CPU %u\n", cpu_policy->cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001359 }
1360
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001361 cpufreq_cpu_put(cpu_policy);
Dave Jonesc9060492008-02-07 16:32:18 -05001362 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001363}
1364
1365/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001366 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 *
1368 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001369 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1370 * restored. It will verify that the current freq is in sync with
1371 * what we believe it to be. This is a bit later than when it
1372 * should be, but nonethteless it's better than calling
1373 * cpufreq_driver->get() here which might re-enable interrupts...
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001374 *
1375 * This function is only executed for the boot CPU. The other CPUs have not
1376 * been turned on yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001378static void cpufreq_bp_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301380 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001381
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001382 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 struct cpufreq_policy *cpu_policy;
1384
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001385 pr_debug("resuming cpu %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001387 /* If there's no policy for the boot CPU, we have nothing to do. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 cpu_policy = cpufreq_cpu_get(cpu);
1389 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001390 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 if (cpufreq_driver->resume) {
1393 ret = cpufreq_driver->resume(cpu_policy);
1394 if (ret) {
1395 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1396 "step on CPU %u\n", cpu_policy->cpu);
Dave Jonesc9060492008-02-07 16:32:18 -05001397 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
1399 }
1400
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 schedule_work(&cpu_policy->update);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001402
Dave Jonesc9060492008-02-07 16:32:18 -05001403fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405}
1406
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001407static struct syscore_ops cpufreq_syscore_ops = {
1408 .suspend = cpufreq_bp_suspend,
1409 .resume = cpufreq_bp_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410};
1411
1412
1413/*********************************************************************
1414 * NOTIFIER LISTS INTERFACE *
1415 *********************************************************************/
1416
1417/**
1418 * cpufreq_register_notifier - register a driver with cpufreq
1419 * @nb: notifier function to register
1420 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1421 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001422 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 * are notified about clock rate changes (once before and once after
1424 * the transition), or a list of drivers that are notified about
1425 * changes in cpufreq policy.
1426 *
1427 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001428 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 */
1430int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1431{
1432 int ret;
1433
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001434 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 switch (list) {
1437 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001438 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001439 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 break;
1441 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001442 ret = blocking_notifier_chain_register(
1443 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 break;
1445 default:
1446 ret = -EINVAL;
1447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 return ret;
1450}
1451EXPORT_SYMBOL(cpufreq_register_notifier);
1452
1453
1454/**
1455 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1456 * @nb: notifier block to be unregistered
1457 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1458 *
1459 * Remove a driver from the CPU frequency notifier list.
1460 *
1461 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001462 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 */
1464int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1465{
1466 int ret;
1467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 switch (list) {
1469 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001470 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001471 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 break;
1473 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001474 ret = blocking_notifier_chain_unregister(
1475 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 break;
1477 default:
1478 ret = -EINVAL;
1479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481 return ret;
1482}
1483EXPORT_SYMBOL(cpufreq_unregister_notifier);
1484
1485
1486/*********************************************************************
1487 * GOVERNORS *
1488 *********************************************************************/
1489
1490
1491int __cpufreq_driver_target(struct cpufreq_policy *policy,
1492 unsigned int target_freq,
1493 unsigned int relation)
1494{
1495 int retval = -EINVAL;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001496
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001497 if (cpufreq_disabled())
1498 return -ENODEV;
1499
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001500 pr_debug("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 target_freq, relation);
1502 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1503 retval = cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 return retval;
1506}
1507EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509int cpufreq_driver_target(struct cpufreq_policy *policy,
1510 unsigned int target_freq,
1511 unsigned int relation)
1512{
Julia Lawallf1829e42008-07-25 22:44:53 +02001513 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
1515 policy = cpufreq_cpu_get(policy->cpu);
1516 if (!policy)
Julia Lawallf1829e42008-07-25 22:44:53 +02001517 goto no_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001519 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
Julia Lawallf1829e42008-07-25 22:44:53 +02001520 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 ret = __cpufreq_driver_target(policy, target_freq, relation);
1523
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001524 unlock_policy_rwsem_write(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Julia Lawallf1829e42008-07-25 22:44:53 +02001526fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 cpufreq_cpu_put(policy);
Julia Lawallf1829e42008-07-25 22:44:53 +02001528no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 return ret;
1530}
1531EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1532
venkatesh.pallipadi@intel.combf0b90e2008-08-04 11:59:07 -07001533int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu)
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001534{
1535 int ret = 0;
1536
1537 policy = cpufreq_cpu_get(policy->cpu);
1538 if (!policy)
1539 return -EINVAL;
1540
venkatesh.pallipadi@intel.combf0b90e2008-08-04 11:59:07 -07001541 if (cpu_online(cpu) && cpufreq_driver->getavg)
1542 ret = cpufreq_driver->getavg(policy, cpu);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001543
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001544 cpufreq_cpu_put(policy);
1545 return ret;
1546}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001547EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001548
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001549/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001550 * when "event" is CPUFREQ_GOV_LIMITS
1551 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301553static int __cpufreq_governor(struct cpufreq_policy *policy,
1554 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555{
Dave Jonescc993ca2005-07-28 09:43:56 -07001556 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001557
1558 /* Only must be defined when default governor is known to have latency
1559 restrictions, like e.g. conservative or ondemand.
1560 That this is the case is already ensured in Kconfig
1561 */
1562#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1563 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1564#else
1565 struct cpufreq_governor *gov = NULL;
1566#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001567
1568 if (policy->governor->max_transition_latency &&
1569 policy->cpuinfo.transition_latency >
1570 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07001571 if (!gov)
1572 return -EINVAL;
1573 else {
1574 printk(KERN_WARNING "%s governor failed, too long"
1575 " transition latency of HW, fallback"
1576 " to %s governor\n",
1577 policy->governor->name,
1578 gov->name);
1579 policy->governor = gov;
1580 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
1583 if (!try_module_get(policy->governor->owner))
1584 return -EINVAL;
1585
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001586 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301587 policy->cpu, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 ret = policy->governor->governor(policy, event);
1589
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301590 /* we keep one module reference alive for
1591 each CPU governed by this CPU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 if ((event != CPUFREQ_GOV_START) || ret)
1593 module_put(policy->governor->owner);
1594 if ((event == CPUFREQ_GOV_STOP) && !ret)
1595 module_put(policy->governor->owner);
1596
1597 return ret;
1598}
1599
1600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601int cpufreq_register_governor(struct cpufreq_governor *governor)
1602{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001603 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605 if (!governor)
1606 return -EINVAL;
1607
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001608 if (cpufreq_disabled())
1609 return -ENODEV;
1610
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001611 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001612
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001613 err = -EBUSY;
1614 if (__find_governor(governor->name) == NULL) {
1615 err = 0;
1616 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
Dave Jones32ee8c32006-02-28 00:43:23 -05001619 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001620 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621}
1622EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1623
1624
1625void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1626{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001627#ifdef CONFIG_HOTPLUG_CPU
1628 int cpu;
1629#endif
1630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 if (!governor)
1632 return;
1633
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001634 if (cpufreq_disabled())
1635 return;
1636
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001637#ifdef CONFIG_HOTPLUG_CPU
1638 for_each_present_cpu(cpu) {
1639 if (cpu_online(cpu))
1640 continue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001641 if (!strcmp(per_cpu(cpufreq_policy_save, cpu).gov,
1642 governor->name))
1643 strcpy(per_cpu(cpufreq_policy_save, cpu).gov, "\0");
1644 per_cpu(cpufreq_policy_save, cpu).min = 0;
1645 per_cpu(cpufreq_policy_save, cpu).max = 0;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001646 }
1647#endif
1648
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001649 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001651 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 return;
1653}
1654EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1655
1656
1657
1658/*********************************************************************
1659 * POLICY INTERFACE *
1660 *********************************************************************/
1661
1662/**
1663 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05001664 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1665 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 *
1667 * Reads the current cpufreq policy.
1668 */
1669int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1670{
1671 struct cpufreq_policy *cpu_policy;
1672 if (!policy)
1673 return -EINVAL;
1674
1675 cpu_policy = cpufreq_cpu_get(cpu);
1676 if (!cpu_policy)
1677 return -EINVAL;
1678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 return 0;
1683}
1684EXPORT_SYMBOL(cpufreq_get_policy);
1685
1686
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001687/*
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301688 * data : current policy.
1689 * policy : policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001690 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301691static int __cpufreq_set_policy(struct cpufreq_policy *data,
1692 struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
1694 int ret = 0;
1695
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001696 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 policy->min, policy->max);
1698
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301699 memcpy(&policy->cpuinfo, &data->cpuinfo,
1700 sizeof(struct cpufreq_cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Yi Yang53391fa2008-01-30 13:33:34 +01001702 if (policy->min > data->max || policy->max < data->min) {
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02001703 ret = -EINVAL;
1704 goto error_out;
1705 }
1706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 /* verify the cpu speed can be set within this limit */
1708 ret = cpufreq_driver->verify(policy);
1709 if (ret)
1710 goto error_out;
1711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001713 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1714 CPUFREQ_ADJUST, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
1716 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001717 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1718 CPUFREQ_INCOMPATIBLE, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 /* verify the cpu speed can be set within this limit,
1721 which might be different to the first one */
1722 ret = cpufreq_driver->verify(policy);
Alan Sterne041c682006-03-27 01:16:30 -08001723 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
1726 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001727 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1728 CPUFREQ_NOTIFY, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Dave Jones7d5e3502006-02-02 17:03:42 -05001730 data->min = policy->min;
1731 data->max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001733 pr_debug("new min and max freqs are %u - %u kHz\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301734 data->min, data->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
1736 if (cpufreq_driver->setpolicy) {
1737 data->policy = policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001738 pr_debug("setting range\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 ret = cpufreq_driver->setpolicy(policy);
1740 } else {
1741 if (policy->governor != data->governor) {
1742 /* save old, working values */
1743 struct cpufreq_governor *old_gov = data->governor;
1744
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001745 pr_debug("governor switch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
1747 /* end old governor */
Andrej Gelenbergffe62752010-05-14 15:15:58 -07001748 if (data->governor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1750
1751 /* start new governor */
1752 data->governor = policy->governor;
1753 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1754 /* new governor failed, so re-start old one */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001755 pr_debug("starting governor %s failed\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301756 data->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 if (old_gov) {
1758 data->governor = old_gov;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301759 __cpufreq_governor(data,
1760 CPUFREQ_GOV_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 }
1762 ret = -EINVAL;
1763 goto error_out;
1764 }
1765 /* might be a policy change, too, so fall through */
1766 }
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001767 pr_debug("governor: change or update limits\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1769 }
1770
Dave Jones7d5e3502006-02-02 17:03:42 -05001771error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 return ret;
1773}
1774
1775/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1777 * @cpu: CPU which shall be re-evaluated
1778 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001779 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 * at different times.
1781 */
1782int cpufreq_update_policy(unsigned int cpu)
1783{
1784 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1785 struct cpufreq_policy policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02001786 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Julia Lawallf1829e42008-07-25 22:44:53 +02001788 if (!data) {
1789 ret = -ENODEV;
1790 goto no_policy;
1791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Julia Lawallf1829e42008-07-25 22:44:53 +02001793 if (unlikely(lock_policy_rwsem_write(cpu))) {
1794 ret = -EINVAL;
1795 goto fail;
1796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001798 pr_debug("updating policy for CPU %u\n", cpu);
Dave Jones7d5e3502006-02-02 17:03:42 -05001799 memcpy(&policy, data, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 policy.min = data->user_policy.min;
1801 policy.max = data->user_policy.max;
1802 policy.policy = data->user_policy.policy;
1803 policy.governor = data->user_policy.governor;
1804
Thomas Renninger0961dd02006-01-26 18:46:33 +01001805 /* BIOS might change freq behind our back
1806 -> ask driver for current freq and notify governors about a change */
1807 if (cpufreq_driver->get) {
1808 policy.cur = cpufreq_driver->get(cpu);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001809 if (!data->cur) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001810 pr_debug("Driver did not initialize current freq");
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001811 data->cur = policy.cur;
1812 } else {
1813 if (data->cur != policy.cur)
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301814 cpufreq_out_of_sync(cpu, data->cur,
1815 policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001816 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01001817 }
1818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 ret = __cpufreq_set_policy(data, &policy);
1820
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001821 unlock_policy_rwsem_write(cpu);
1822
Julia Lawallf1829e42008-07-25 22:44:53 +02001823fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 cpufreq_cpu_put(data);
Julia Lawallf1829e42008-07-25 22:44:53 +02001825no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 return ret;
1827}
1828EXPORT_SYMBOL(cpufreq_update_policy);
1829
Satyam Sharmadd184a02007-10-02 13:28:14 -07001830static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08001831 unsigned long action, void *hcpu)
1832{
1833 unsigned int cpu = (unsigned long)hcpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001834 struct device *dev;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001835
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001836 dev = get_cpu_device(cpu);
1837 if (dev) {
Ashok Rajc32b6b82005-10-30 14:59:54 -08001838 switch (action) {
1839 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001840 case CPU_ONLINE_FROZEN:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001841 cpufreq_add_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001842 break;
1843 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001844 case CPU_DOWN_PREPARE_FROZEN:
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001845 if (unlikely(lock_policy_rwsem_write(cpu)))
1846 BUG();
1847
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001848 __cpufreq_remove_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001849 break;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001850 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001851 case CPU_DOWN_FAILED_FROZEN:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001852 cpufreq_add_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001853 break;
1854 }
1855 }
1856 return NOTIFY_OK;
1857}
1858
Neal Buckendahl9c36f742010-06-22 22:02:44 -05001859static struct notifier_block __refdata cpufreq_cpu_notifier = {
Ashok Rajc32b6b82005-10-30 14:59:54 -08001860 .notifier_call = cpufreq_cpu_callback,
1861};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863/*********************************************************************
1864 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1865 *********************************************************************/
1866
1867/**
1868 * cpufreq_register_driver - register a CPU Frequency driver
1869 * @driver_data: A struct cpufreq_driver containing the values#
1870 * submitted by the CPU Frequency driver.
1871 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001872 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05001874 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 *
1876 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001877int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
1879 unsigned long flags;
1880 int ret;
1881
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001882 if (cpufreq_disabled())
1883 return -ENODEV;
1884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 if (!driver_data || !driver_data->verify || !driver_data->init ||
1886 ((!driver_data->setpolicy) && (!driver_data->target)))
1887 return -EINVAL;
1888
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001889 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
1891 if (driver_data->setpolicy)
1892 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1893
1894 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1895 if (cpufreq_driver) {
1896 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1897 return -EBUSY;
1898 }
1899 cpufreq_driver = driver_data;
1900 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1901
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001902 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001903 if (ret)
1904 goto err_null_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001906 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 int i;
1908 ret = -ENODEV;
1909
1910 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07001911 for (i = 0; i < nr_cpu_ids; i++)
1912 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07001914 break;
1915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
1917 /* if all ->init() calls failed, unregister */
1918 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001919 pr_debug("no CPU initialized for driver %s\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301920 driver_data->name);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001921 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 }
1923 }
1924
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001925 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001926 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001928 return 0;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001929err_if_unreg:
1930 subsys_interface_unregister(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001931err_null_driver:
1932 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1933 cpufreq_driver = NULL;
1934 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05001935 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936}
1937EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1938
1939
1940/**
1941 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1942 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001943 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 * the right to do so, i.e. if you have succeeded in initialising before!
1945 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1946 * currently not initialised.
1947 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001948int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
1950 unsigned long flags;
1951
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001952 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001955 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001957 subsys_interface_unregister(&cpufreq_interface);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001958 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
1960 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1961 cpufreq_driver = NULL;
1962 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1963
1964 return 0;
1965}
1966EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001967
1968static int __init cpufreq_core_init(void)
1969{
1970 int cpu;
1971
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001972 if (cpufreq_disabled())
1973 return -ENODEV;
1974
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001975 for_each_possible_cpu(cpu) {
Tejun Heof1625062009-10-29 22:34:13 +09001976 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001977 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
1978 }
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02001979
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001980 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02001981 BUG_ON(!cpufreq_global_kobject);
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001982 register_syscore_ops(&cpufreq_syscore_ops);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02001983
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001984 return 0;
1985}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001986core_initcall(cpufreq_core_init);