blob: a0a33bdb4533ccd85aa7712f4a1b6529b5bb73d1 [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
Viresh Kumardb701152012-10-23 01:29:03 +020018#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/notifier.h>
24#include <linux/cpufreq.h>
25#include <linux/delay.h>
26#include <linux/interrupt.h>
27#include <linux/spinlock.h>
28#include <linux/device.h>
29#include <linux/slab.h>
30#include <linux/cpu.h>
31#include <linux/completion.h>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080032#include <linux/mutex.h>
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +010033#include <linux/syscore_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Thomas Renninger6f4f2722010-04-20 13:17:36 +020035#include <trace/events/power.h>
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/**
Dave Jonescd878472006-08-11 17:59:28 -040038 * The "cpufreq driver" - the arch- or hardware-dependent low
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * level driver of CPUFreq support, and its spinlock. This lock
40 * also protects the cpufreq_cpu_data array.
41 */
Dave Jones7d5e3502006-02-02 17:03:42 -050042static struct cpufreq_driver *cpufreq_driver;
Mike Travis7a6aedf2008-03-25 15:06:53 -070043static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
Thomas Renninger084f3492007-07-09 11:35:28 -070044#ifdef CONFIG_HOTPLUG_CPU
45/* This one keeps track of the previously set governor of a removed CPU */
Dmitry Monakhove77b89f2009-10-05 00:38:55 +040046static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
Thomas Renninger084f3492007-07-09 11:35:28 -070047#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static DEFINE_SPINLOCK(cpufreq_driver_lock);
49
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080050/*
51 * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
52 * all cpufreq/hotplug/workqueue/etc related lock issues.
53 *
54 * The rules for this semaphore:
55 * - Any routine that wants to read from the policy structure will
56 * do a down_read on this semaphore.
57 * - Any routine that will write to the policy structure and/or may take away
58 * the policy altogether (eg. CPU hotplug), will hold this lock in write
59 * mode before doing so.
60 *
61 * Additional rules:
62 * - All holders of the lock should check to make sure that the CPU they
63 * are concerned with are online after they get the lock.
64 * - Governor routines that can be called in cpufreq hotplug path should not
65 * take this sem as top level hotplug notifier handler takes this.
Mathieu Desnoyers395913d2009-06-08 13:17:31 -040066 * - Lock should not be held across
67 * __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080068 */
Tejun Heof1625062009-10-29 22:34:13 +090069static DEFINE_PER_CPU(int, cpufreq_policy_cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080070static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
71
72#define lock_policy_rwsem(mode, cpu) \
Amerigo Wang226528c2010-03-04 03:23:36 -050073static int lock_policy_rwsem_##mode \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080074(int cpu) \
75{ \
Tejun Heof1625062009-10-29 22:34:13 +090076 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080077 BUG_ON(policy_cpu == -1); \
78 down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
79 if (unlikely(!cpu_online(cpu))) { \
80 up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
81 return -1; \
82 } \
83 \
84 return 0; \
85}
86
87lock_policy_rwsem(read, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080088
89lock_policy_rwsem(write, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080090
Amerigo Wang226528c2010-03-04 03:23:36 -050091static void unlock_policy_rwsem_read(int cpu)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080092{
Tejun Heof1625062009-10-29 22:34:13 +090093 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080094 BUG_ON(policy_cpu == -1);
95 up_read(&per_cpu(cpu_policy_rwsem, policy_cpu));
96}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080097
Amerigo Wang226528c2010-03-04 03:23:36 -050098static void unlock_policy_rwsem_write(int cpu)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080099{
Tejun Heof1625062009-10-29 22:34:13 +0900100 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800101 BUG_ON(policy_cpu == -1);
102 up_write(&per_cpu(cpu_policy_rwsem, policy_cpu));
103}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800104
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106/* internal prototypes */
Dave Jones29464f22009-01-18 01:37:11 -0500107static int __cpufreq_governor(struct cpufreq_policy *policy,
108 unsigned int event);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800109static unsigned int __cpufreq_get(unsigned int cpu);
David Howells65f27f32006-11-22 14:55:48 +0000110static void handle_update(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112/**
Dave Jones32ee8c32006-02-28 00:43:23 -0500113 * Two notifier lists: the "policy" list is involved in the
114 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 * "transition" list for kernel code that needs to handle
116 * changes to devices when the CPU clock speed changes.
117 * The mutex locks both lists.
118 */
Alan Sterne041c682006-03-27 01:16:30 -0800119static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700120static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200122static bool init_cpufreq_transition_notifier_list_called;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700123static int __init init_cpufreq_transition_notifier_list(void)
124{
125 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200126 init_cpufreq_transition_notifier_list_called = true;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700127 return 0;
128}
Linus Torvaldsb3438f82006-11-20 11:47:18 -0800129pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400131static int off __read_mostly;
Viresh Kumarda584452012-10-26 00:51:32 +0200132static int cpufreq_disabled(void)
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400133{
134 return off;
135}
136void disable_cpufreq(void)
137{
138 off = 1;
139}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140static LIST_HEAD(cpufreq_governor_list);
Dave Jones29464f22009-01-18 01:37:11 -0500141static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Stephen Boyda9144432012-07-20 18:14:38 +0000143static struct cpufreq_policy *__cpufreq_cpu_get(unsigned int cpu, bool sysfs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 struct cpufreq_policy *data;
146 unsigned long flags;
147
Mike Travis7a6aedf2008-03-25 15:06:53 -0700148 if (cpu >= nr_cpu_ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 goto err_out;
150
151 /* get the cpufreq driver */
152 spin_lock_irqsave(&cpufreq_driver_lock, flags);
153
154 if (!cpufreq_driver)
155 goto err_out_unlock;
156
157 if (!try_module_get(cpufreq_driver->owner))
158 goto err_out_unlock;
159
160
161 /* get the CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -0700162 data = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 if (!data)
165 goto err_out_put_module;
166
Stephen Boyda9144432012-07-20 18:14:38 +0000167 if (!sysfs && !kobject_get(&data->kobj))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 goto err_out_put_module;
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return data;
172
Dave Jones7d5e3502006-02-02 17:03:42 -0500173err_out_put_module:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 module_put(cpufreq_driver->owner);
Dave Jones7d5e3502006-02-02 17:03:42 -0500175err_out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones7d5e3502006-02-02 17:03:42 -0500177err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return NULL;
179}
Stephen Boyda9144432012-07-20 18:14:38 +0000180
181struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
182{
183 return __cpufreq_cpu_get(cpu, false);
184}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
186
Stephen Boyda9144432012-07-20 18:14:38 +0000187static struct cpufreq_policy *cpufreq_cpu_get_sysfs(unsigned int cpu)
188{
189 return __cpufreq_cpu_get(cpu, true);
190}
191
192static void __cpufreq_cpu_put(struct cpufreq_policy *data, bool sysfs)
193{
194 if (!sysfs)
195 kobject_put(&data->kobj);
196 module_put(cpufreq_driver->owner);
197}
Dave Jones7d5e3502006-02-02 17:03:42 -0500198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199void cpufreq_cpu_put(struct cpufreq_policy *data)
200{
Stephen Boyda9144432012-07-20 18:14:38 +0000201 __cpufreq_cpu_put(data, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
204
Stephen Boyda9144432012-07-20 18:14:38 +0000205static void cpufreq_cpu_put_sysfs(struct cpufreq_policy *data)
206{
207 __cpufreq_cpu_put(data, true);
208}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
212 *********************************************************************/
213
214/**
215 * adjust_jiffies - adjust the system "loops_per_jiffy"
216 *
217 * This function alters the system "loops_per_jiffy" for the clock
218 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500219 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 * per-CPU loops_per_jiffy value wherever possible.
221 */
222#ifndef CONFIG_SMP
223static unsigned long l_p_j_ref;
224static unsigned int l_p_j_ref_freq;
225
Arjan van de Ven858119e2006-01-14 13:20:43 -0800226static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 if (ci->flags & CPUFREQ_CONST_LOOPS)
229 return;
230
231 if (!l_p_j_ref_freq) {
232 l_p_j_ref = loops_per_jiffy;
233 l_p_j_ref_freq = ci->old;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200234 pr_debug("saving %lu as reference value for loops_per_jiffy; "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530235 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
Afzal Mohammedd08de0c12012-01-04 10:52:46 +0530237 if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700238 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530239 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
240 ci->new);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200241 pr_debug("scaling loops_per_jiffy to %lu "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530242 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244}
245#else
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530246static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
247{
248 return;
249}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250#endif
251
252
253/**
Dave Jonese4472cb2006-01-31 15:53:55 -0800254 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
255 * on frequency transition.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 *
Dave Jonese4472cb2006-01-31 15:53:55 -0800257 * This function calls the transition notifiers and the "adjust_jiffies"
258 * function. It is called twice on all CPU frequency changes that have
Dave Jones32ee8c32006-02-28 00:43:23 -0500259 * external effects.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 */
261void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
262{
Dave Jonese4472cb2006-01-31 15:53:55 -0800263 struct cpufreq_policy *policy;
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 BUG_ON(irqs_disabled());
266
267 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200268 pr_debug("notification %u of frequency transition to %u kHz\n",
Dave Jonese4472cb2006-01-31 15:53:55 -0800269 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Mike Travis7a6aedf2008-03-25 15:06:53 -0700271 policy = per_cpu(cpufreq_cpu_data, freqs->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500275 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800276 * which is not equal to what the cpufreq core thinks is
277 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 */
279 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800280 if ((policy) && (policy->cpu == freqs->cpu) &&
281 (policy->cur) && (policy->cur != freqs->old)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200282 pr_debug("Warning: CPU frequency is"
Dave Jonese4472cb2006-01-31 15:53:55 -0800283 " %u, cpufreq assumed %u kHz.\n",
284 freqs->old, policy->cur);
285 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700288 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800289 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
291 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 case CPUFREQ_POSTCHANGE:
294 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200295 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
Thomas Renninger6f4f2722010-04-20 13:17:36 +0200296 (unsigned long)freqs->cpu);
297 trace_power_frequency(POWER_PSTATE, freqs->new, freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100298 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700299 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800300 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800301 if (likely(policy) && likely(policy->cpu == freqs->cpu))
302 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 break;
304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
307
308
309
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);
396
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530397static int __cpufreq_set_policy(struct cpufreq_policy *data,
398 struct cpufreq_policy *policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400/**
401 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
402 */
403#define store_one(file_name, object) \
404static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500405(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{ \
Jingoo Hanf55c9c22012-10-31 05:49:13 +0000407 unsigned int ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 struct cpufreq_policy new_policy; \
409 \
410 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
411 if (ret) \
412 return -EINVAL; \
413 \
Dave Jones29464f22009-01-18 01:37:11 -0500414 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (ret != 1) \
416 return -EINVAL; \
417 \
Thomas Renninger7970e082006-04-13 15:14:04 +0200418 ret = __cpufreq_set_policy(policy, &new_policy); \
419 policy->user_policy.object = policy->object; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 \
421 return ret ? ret : count; \
422}
423
Dave Jones29464f22009-01-18 01:37:11 -0500424store_one(scaling_min_freq, min);
425store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427/**
428 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
429 */
Dave Jones905d77c2008-03-05 14:28:32 -0500430static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
431 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800433 unsigned int cur_freq = __cpufreq_get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (!cur_freq)
435 return sprintf(buf, "<unknown>");
436 return sprintf(buf, "%u\n", cur_freq);
437}
438
439
440/**
441 * show_scaling_governor - show the current policy for the specified CPU
442 */
Dave Jones905d77c2008-03-05 14:28:32 -0500443static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Dave Jones29464f22009-01-18 01:37:11 -0500445 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return sprintf(buf, "powersave\n");
447 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
448 return sprintf(buf, "performance\n");
449 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200450 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500451 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return -EINVAL;
453}
454
455
456/**
457 * store_scaling_governor - store policy for the specified CPU
458 */
Dave Jones905d77c2008-03-05 14:28:32 -0500459static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
460 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Jingoo Hanf55c9c22012-10-31 05:49:13 +0000462 unsigned int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 char str_governor[16];
464 struct cpufreq_policy new_policy;
465
466 ret = cpufreq_get_policy(&new_policy, policy->cpu);
467 if (ret)
468 return ret;
469
Dave Jones29464f22009-01-18 01:37:11 -0500470 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (ret != 1)
472 return -EINVAL;
473
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530474 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
475 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return -EINVAL;
477
Thomas Renninger7970e082006-04-13 15:14:04 +0200478 /* Do not use cpufreq_set_policy here or the user_policy.max
479 will be wrongly overridden */
Thomas Renninger7970e082006-04-13 15:14:04 +0200480 ret = __cpufreq_set_policy(policy, &new_policy);
481
482 policy->user_policy.policy = policy->policy;
483 policy->user_policy.governor = policy->governor;
Thomas Renninger7970e082006-04-13 15:14:04 +0200484
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530485 if (ret)
486 return ret;
487 else
488 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
491/**
492 * show_scaling_driver - show the cpufreq driver currently loaded
493 */
Dave Jones905d77c2008-03-05 14:28:32 -0500494static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
viresh kumar4b972f02012-10-23 01:23:43 +0200496 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
499/**
500 * show_scaling_available_governors - show the available CPUfreq governors
501 */
Dave Jones905d77c2008-03-05 14:28:32 -0500502static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
503 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 ssize_t i = 0;
506 struct cpufreq_governor *t;
507
508 if (!cpufreq_driver->target) {
509 i += sprintf(buf, "performance powersave");
510 goto out;
511 }
512
513 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
Dave Jones29464f22009-01-18 01:37:11 -0500514 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
515 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200517 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500519out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 i += sprintf(&buf[i], "\n");
521 return i;
522}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700523
Rusty Russell835481d2009-01-04 05:18:06 -0800524static ssize_t show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
526 ssize_t i = 0;
527 unsigned int cpu;
528
Rusty Russell835481d2009-01-04 05:18:06 -0800529 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (i)
531 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
532 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
533 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500534 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
536 i += sprintf(&buf[i], "\n");
537 return i;
538}
539
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700540/**
541 * show_related_cpus - show the CPUs affected by each transition even if
542 * hw coordination is in use
543 */
544static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
545{
Rusty Russell835481d2009-01-04 05:18:06 -0800546 if (cpumask_empty(policy->related_cpus))
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700547 return show_cpus(policy->cpus, buf);
548 return show_cpus(policy->related_cpus, buf);
549}
550
551/**
552 * show_affected_cpus - show the CPUs affected by each transition
553 */
554static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
555{
556 return show_cpus(policy->cpus, buf);
557}
558
Venki Pallipadi9e769882007-10-26 10:18:21 -0700559static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500560 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700561{
562 unsigned int freq = 0;
563 unsigned int ret;
564
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700565 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700566 return -EINVAL;
567
568 ret = sscanf(buf, "%u", &freq);
569 if (ret != 1)
570 return -EINVAL;
571
572 policy->governor->store_setspeed(policy, freq);
573
574 return count;
575}
576
577static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
578{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700579 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700580 return sprintf(buf, "<unsupported>\n");
581
582 return policy->governor->show_setspeed(policy, buf);
583}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Thomas Renningere2f74f32009-11-19 12:31:01 +0100585/**
viresh kumar8bf1ac722012-10-23 01:23:33 +0200586 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100587 */
588static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
589{
590 unsigned int limit;
591 int ret;
592 if (cpufreq_driver->bios_limit) {
593 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
594 if (!ret)
595 return sprintf(buf, "%u\n", limit);
596 }
597 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
598}
599
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200600cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
601cpufreq_freq_attr_ro(cpuinfo_min_freq);
602cpufreq_freq_attr_ro(cpuinfo_max_freq);
603cpufreq_freq_attr_ro(cpuinfo_transition_latency);
604cpufreq_freq_attr_ro(scaling_available_governors);
605cpufreq_freq_attr_ro(scaling_driver);
606cpufreq_freq_attr_ro(scaling_cur_freq);
607cpufreq_freq_attr_ro(bios_limit);
608cpufreq_freq_attr_ro(related_cpus);
609cpufreq_freq_attr_ro(affected_cpus);
610cpufreq_freq_attr_rw(scaling_min_freq);
611cpufreq_freq_attr_rw(scaling_max_freq);
612cpufreq_freq_attr_rw(scaling_governor);
613cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Dave Jones905d77c2008-03-05 14:28:32 -0500615static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 &cpuinfo_min_freq.attr,
617 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100618 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 &scaling_min_freq.attr,
620 &scaling_max_freq.attr,
621 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700622 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 &scaling_governor.attr,
624 &scaling_driver.attr,
625 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700626 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 NULL
628};
629
Thomas Renninger8aa84ad2009-07-24 15:25:05 +0200630struct kobject *cpufreq_global_kobject;
631EXPORT_SYMBOL(cpufreq_global_kobject);
632
Dave Jones29464f22009-01-18 01:37:11 -0500633#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
634#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Dave Jones29464f22009-01-18 01:37:11 -0500636static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Dave Jones905d77c2008-03-05 14:28:32 -0500638 struct cpufreq_policy *policy = to_policy(kobj);
639 struct freq_attr *fattr = to_attr(attr);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500640 ssize_t ret = -EINVAL;
Stephen Boyda9144432012-07-20 18:14:38 +0000641 policy = cpufreq_cpu_get_sysfs(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (!policy)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500643 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800644
645 if (lock_policy_rwsem_read(policy->cpu) < 0)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500646 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800647
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530648 if (fattr->show)
649 ret = fattr->show(policy, buf);
650 else
651 ret = -EIO;
652
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800653 unlock_policy_rwsem_read(policy->cpu);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500654fail:
Stephen Boyda9144432012-07-20 18:14:38 +0000655 cpufreq_cpu_put_sysfs(policy);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500656no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return ret;
658}
659
Dave Jones905d77c2008-03-05 14:28:32 -0500660static ssize_t store(struct kobject *kobj, struct attribute *attr,
661 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Dave Jones905d77c2008-03-05 14:28:32 -0500663 struct cpufreq_policy *policy = to_policy(kobj);
664 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500665 ssize_t ret = -EINVAL;
Stephen Boyda9144432012-07-20 18:14:38 +0000666 policy = cpufreq_cpu_get_sysfs(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (!policy)
Dave Jonesa07530b2008-03-05 14:22:25 -0500668 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800669
670 if (lock_policy_rwsem_write(policy->cpu) < 0)
Dave Jonesa07530b2008-03-05 14:22:25 -0500671 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800672
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530673 if (fattr->store)
674 ret = fattr->store(policy, buf, count);
675 else
676 ret = -EIO;
677
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800678 unlock_policy_rwsem_write(policy->cpu);
Dave Jonesa07530b2008-03-05 14:22:25 -0500679fail:
Stephen Boyda9144432012-07-20 18:14:38 +0000680 cpufreq_cpu_put_sysfs(policy);
Dave Jonesa07530b2008-03-05 14:22:25 -0500681no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return ret;
683}
684
Dave Jones905d77c2008-03-05 14:28:32 -0500685static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Dave Jones905d77c2008-03-05 14:28:32 -0500687 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200688 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 complete(&policy->kobj_unregister);
690}
691
Emese Revfy52cf25d2010-01-19 02:58:23 +0100692static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 .show = show,
694 .store = store,
695};
696
697static struct kobj_type ktype_cpufreq = {
698 .sysfs_ops = &sysfs_ops,
699 .default_attrs = default_attrs,
700 .release = cpufreq_sysfs_release,
701};
702
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200703/*
704 * Returns:
705 * Negative: Failure
706 * 0: Success
707 * Positive: When we have a managed CPU and the sysfs got symlinked
708 */
Alex Chiangcf3289d02009-11-17 20:27:08 -0700709static int cpufreq_add_dev_policy(unsigned int cpu,
710 struct cpufreq_policy *policy,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800711 struct device *dev)
Dave Jonesecf7e462009-07-08 18:48:47 -0400712{
713 int ret = 0;
714#ifdef CONFIG_SMP
715 unsigned long flags;
716 unsigned int j;
Dave Jonesecf7e462009-07-08 18:48:47 -0400717#ifdef CONFIG_HOTPLUG_CPU
Dmitry Monakhove77b89f2009-10-05 00:38:55 +0400718 struct cpufreq_governor *gov;
719
720 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
721 if (gov) {
722 policy->governor = gov;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200723 pr_debug("Restoring governor %s for cpu %d\n",
Dave Jonesecf7e462009-07-08 18:48:47 -0400724 policy->governor->name, cpu);
725 }
726#endif
727
728 for_each_cpu(j, policy->cpus) {
729 struct cpufreq_policy *managed_policy;
730
731 if (cpu == j)
732 continue;
733
734 /* Check for existing affected CPUs.
735 * They may not be aware of it due to CPU Hotplug.
736 * cpufreq_cpu_put is called when the device is removed
737 * in __cpufreq_remove_dev()
738 */
739 managed_policy = cpufreq_cpu_get(j);
740 if (unlikely(managed_policy)) {
741
742 /* Set proper policy_cpu */
743 unlock_policy_rwsem_write(cpu);
Tejun Heof1625062009-10-29 22:34:13 +0900744 per_cpu(cpufreq_policy_cpu, cpu) = managed_policy->cpu;
Dave Jonesecf7e462009-07-08 18:48:47 -0400745
746 if (lock_policy_rwsem_write(cpu) < 0) {
747 /* Should not go through policy unlock path */
748 if (cpufreq_driver->exit)
749 cpufreq_driver->exit(policy);
750 cpufreq_cpu_put(managed_policy);
751 return -EBUSY;
752 }
753
Viresh Kumarf6a74092013-01-12 05:14:39 +0000754 __cpufreq_governor(managed_policy, CPUFREQ_GOV_STOP);
755
Dave Jonesecf7e462009-07-08 18:48:47 -0400756 spin_lock_irqsave(&cpufreq_driver_lock, flags);
757 cpumask_copy(managed_policy->cpus, policy->cpus);
758 per_cpu(cpufreq_cpu_data, cpu) = managed_policy;
759 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
760
Viresh Kumarf6a74092013-01-12 05:14:39 +0000761 __cpufreq_governor(managed_policy, CPUFREQ_GOV_START);
762 __cpufreq_governor(managed_policy, CPUFREQ_GOV_LIMITS);
763
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200764 pr_debug("CPU already managed, adding link\n");
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800765 ret = sysfs_create_link(&dev->kobj,
Dave Jonesecf7e462009-07-08 18:48:47 -0400766 &managed_policy->kobj,
767 "cpufreq");
768 if (ret)
769 cpufreq_cpu_put(managed_policy);
770 /*
771 * Success. We only needed to be added to the mask.
772 * Call driver->exit() because only the cpu parent of
773 * the kobj needed to call init().
774 */
775 if (cpufreq_driver->exit)
776 cpufreq_driver->exit(policy);
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200777
778 if (!ret)
779 return 1;
780 else
781 return ret;
Dave Jonesecf7e462009-07-08 18:48:47 -0400782 }
783 }
784#endif
785 return ret;
786}
787
788
Dave Jones19d6f7e2009-07-08 17:35:39 -0400789/* symlink affected CPUs */
Alex Chiangcf3289d02009-11-17 20:27:08 -0700790static int cpufreq_add_dev_symlink(unsigned int cpu,
791 struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400792{
793 unsigned int j;
794 int ret = 0;
795
796 for_each_cpu(j, policy->cpus) {
797 struct cpufreq_policy *managed_policy;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800798 struct device *cpu_dev;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400799
800 if (j == cpu)
801 continue;
802 if (!cpu_online(j))
803 continue;
804
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200805 pr_debug("CPU %u already managed, adding link\n", j);
Dave Jones19d6f7e2009-07-08 17:35:39 -0400806 managed_policy = cpufreq_cpu_get(cpu);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800807 cpu_dev = get_cpu_device(j);
808 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Dave Jones19d6f7e2009-07-08 17:35:39 -0400809 "cpufreq");
810 if (ret) {
811 cpufreq_cpu_put(managed_policy);
812 return ret;
813 }
814 }
815 return ret;
816}
817
Alex Chiangcf3289d02009-11-17 20:27:08 -0700818static int cpufreq_add_dev_interface(unsigned int cpu,
819 struct cpufreq_policy *policy,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800820 struct device *dev)
Dave Jones909a6942009-07-08 18:05:42 -0400821{
Dave Jonesecf7e462009-07-08 18:48:47 -0400822 struct cpufreq_policy new_policy;
Dave Jones909a6942009-07-08 18:05:42 -0400823 struct freq_attr **drv_attr;
824 unsigned long flags;
825 int ret = 0;
826 unsigned int j;
827
828 /* prepare interface data */
829 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800830 &dev->kobj, "cpufreq");
Dave Jones909a6942009-07-08 18:05:42 -0400831 if (ret)
832 return ret;
833
834 /* set up files for this cpu device */
835 drv_attr = cpufreq_driver->attr;
836 while ((drv_attr) && (*drv_attr)) {
837 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
838 if (ret)
839 goto err_out_kobj_put;
840 drv_attr++;
841 }
842 if (cpufreq_driver->get) {
843 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
844 if (ret)
845 goto err_out_kobj_put;
846 }
847 if (cpufreq_driver->target) {
848 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
849 if (ret)
850 goto err_out_kobj_put;
851 }
Thomas Renningere2f74f32009-11-19 12:31:01 +0100852 if (cpufreq_driver->bios_limit) {
853 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
854 if (ret)
855 goto err_out_kobj_put;
856 }
Dave Jones909a6942009-07-08 18:05:42 -0400857
858 spin_lock_irqsave(&cpufreq_driver_lock, flags);
859 for_each_cpu(j, policy->cpus) {
Julia Lawallbec037a2010-08-05 22:23:00 +0200860 if (!cpu_online(j))
861 continue;
Dave Jones909a6942009-07-08 18:05:42 -0400862 per_cpu(cpufreq_cpu_data, j) = policy;
Tejun Heof1625062009-10-29 22:34:13 +0900863 per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
Dave Jones909a6942009-07-08 18:05:42 -0400864 }
865 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
866
867 ret = cpufreq_add_dev_symlink(cpu, policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400868 if (ret)
869 goto err_out_kobj_put;
870
871 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
872 /* assure that the starting sequence is run in __cpufreq_set_policy */
873 policy->governor = NULL;
874
875 /* set default policy */
876 ret = __cpufreq_set_policy(policy, &new_policy);
877 policy->user_policy.policy = policy->policy;
878 policy->user_policy.governor = policy->governor;
879
880 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200881 pr_debug("setting policy failed\n");
Dave Jonesecf7e462009-07-08 18:48:47 -0400882 if (cpufreq_driver->exit)
883 cpufreq_driver->exit(policy);
884 }
Dave Jones909a6942009-07-08 18:05:42 -0400885 return ret;
886
887err_out_kobj_put:
888 kobject_put(&policy->kobj);
889 wait_for_completion(&policy->kobj_unregister);
890 return ret;
891}
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894/**
895 * cpufreq_add_dev - add a CPU device
896 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500897 * Adds the cpufreq interface for a CPU device.
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400898 *
899 * The Oracle says: try running cpufreq registration/unregistration concurrently
900 * with with cpu hotplugging and all hell will break loose. Tried to clean this
901 * mess up, but more thorough testing is needed. - Mathieu
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800903static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800905 unsigned int cpu = dev->id;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500906 int ret = 0, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 unsigned long flags;
909 unsigned int j;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500910#ifdef CONFIG_HOTPLUG_CPU
911 int sibling;
912#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Ashok Rajc32b6b82005-10-30 14:59:54 -0800914 if (cpu_is_offline(cpu))
915 return 0;
916
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200917 pr_debug("adding CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919#ifdef CONFIG_SMP
920 /* check whether a different CPU already registered this
921 * CPU because it is in the same boat. */
922 policy = cpufreq_cpu_get(cpu);
923 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -0500924 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return 0;
926 }
927#endif
928
929 if (!try_module_get(cpufreq_driver->owner)) {
930 ret = -EINVAL;
931 goto module_out;
932 }
933
Dave Jones059019a2009-07-08 16:30:03 -0400934 ret = -ENOMEM;
Dave Jonese98df502005-10-20 15:17:43 -0700935 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
Dave Jones059019a2009-07-08 16:30:03 -0400936 if (!policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 goto nomem_out;
Dave Jones059019a2009-07-08 16:30:03 -0400938
939 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400940 goto err_free_policy;
Dave Jones059019a2009-07-08 16:30:03 -0400941
942 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400943 goto err_free_cpumask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 policy->cpu = cpu;
Rusty Russell835481d2009-01-04 05:18:06 -0800946 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800948 /* Initially set CPU itself as the policy_cpu */
Tejun Heof1625062009-10-29 22:34:13 +0900949 per_cpu(cpufreq_policy_cpu, cpu) = cpu;
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400950 ret = (lock_policy_rwsem_write(cpu) < 0);
951 WARN_ON(ret);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 init_completion(&policy->kobj_unregister);
David Howells65f27f32006-11-22 14:55:48 +0000954 INIT_WORK(&policy->update, handle_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Thomas Renninger8122c6c2007-10-02 13:28:09 -0700956 /* Set governor before ->init, so that driver could check it */
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500957#ifdef CONFIG_HOTPLUG_CPU
958 for_each_online_cpu(sibling) {
959 struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
960 if (cp && cp->governor &&
961 (cpumask_test_cpu(cpu, cp->related_cpus))) {
962 policy->governor = cp->governor;
963 found = 1;
964 break;
965 }
966 }
967#endif
968 if (!found)
969 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 /* call driver. From then on the cpufreq must be able
971 * to accept all calls to ->verify and ->setpolicy for this CPU
972 */
973 ret = cpufreq_driver->init(policy);
974 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200975 pr_debug("initialization failed\n");
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400976 goto err_unlock_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 }
Viresh Kumar643ae6e2013-01-12 05:14:38 +0000978
979 /*
980 * affected cpus must always be the one, which are online. We aren't
981 * managing offline cpus here.
982 */
983 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
984
Mike Chan187d9f42008-12-04 12:19:17 -0800985 policy->user_policy.min = policy->min;
986 policy->user_policy.max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Thomas Renningera1531ac2008-07-29 22:32:58 -0700988 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
989 CPUFREQ_START, policy);
990
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800991 ret = cpufreq_add_dev_policy(cpu, policy, dev);
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200992 if (ret) {
993 if (ret > 0)
994 /* This is a managed cpu, symlink created,
995 exit with 0 */
996 ret = 0;
Dave Jonesecf7e462009-07-08 18:48:47 -0400997 goto err_unlock_policy;
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001000 ret = cpufreq_add_dev_interface(cpu, policy, dev);
Dave Jones19d6f7e2009-07-08 17:35:39 -04001001 if (ret)
1002 goto err_out_unregister;
Dave Jones8ff69732006-03-05 03:37:23 -05001003
Lothar Waßmanndca02612008-05-29 17:54:52 +02001004 unlock_policy_rwsem_write(cpu);
1005
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001006 kobject_uevent(&policy->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 module_put(cpufreq_driver->owner);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001008 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return 0;
1011
1012
1013err_out_unregister:
1014 spin_lock_irqsave(&cpufreq_driver_lock, flags);
Rusty Russell835481d2009-01-04 05:18:06 -08001015 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -07001016 per_cpu(cpufreq_cpu_data, j) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1018
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -08001019 kobject_put(&policy->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 wait_for_completion(&policy->kobj_unregister);
1021
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001022err_unlock_policy:
Dave Jones45709112008-03-05 14:07:34 -05001023 unlock_policy_rwsem_write(cpu);
Xiaotian Fengcad70a62010-07-20 20:11:02 +08001024 free_cpumask_var(policy->related_cpus);
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001025err_free_cpumask:
1026 free_cpumask_var(policy->cpus);
1027err_free_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 kfree(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029nomem_out:
1030 module_put(cpufreq_driver->owner);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001031module_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 return ret;
1033}
1034
1035
1036/**
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001037 * __cpufreq_remove_dev - remove a CPU device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 *
1039 * Removes the cpufreq interface for a CPU device.
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001040 * Caller should already have policy_rwsem in write mode for this CPU.
1041 * This routine frees the rwsem before returning.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001043static int __cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001045 unsigned int cpu = dev->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 unsigned long flags;
1047 struct cpufreq_policy *data;
Amerigo Wang499bca92010-03-04 03:23:46 -05001048 struct kobject *kobj;
1049 struct completion *cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050#ifdef CONFIG_SMP
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001051 struct device *cpu_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 unsigned int j;
1053#endif
1054
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001055 pr_debug("unregistering CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 spin_lock_irqsave(&cpufreq_driver_lock, flags);
Mike Travis7a6aedf2008-03-25 15:06:53 -07001058 data = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 if (!data) {
1061 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001062 unlock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return -EINVAL;
1064 }
Mike Travis7a6aedf2008-03-25 15:06:53 -07001065 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067
1068#ifdef CONFIG_SMP
1069 /* if this isn't the CPU which is the parent of the kobj, we
Dave Jones32ee8c32006-02-28 00:43:23 -05001070 * only need to unlink, put and exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 */
1072 if (unlikely(cpu != data->cpu)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001073 pr_debug("removing link\n");
Viresh Kumarf6a74092013-01-12 05:14:39 +00001074 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Rusty Russell835481d2009-01-04 05:18:06 -08001075 cpumask_clear_cpu(cpu, data->cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarf6a74092013-01-12 05:14:39 +00001077
1078 __cpufreq_governor(data, CPUFREQ_GOV_START);
1079 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1080
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001081 kobj = &dev->kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 cpufreq_cpu_put(data);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001083 unlock_policy_rwsem_write(cpu);
Amerigo Wang499bca92010-03-04 03:23:46 -05001084 sysfs_remove_link(kobj, "cpufreq");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return 0;
1086 }
1087#endif
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089#ifdef CONFIG_SMP
Thomas Renninger084f3492007-07-09 11:35:28 -07001090
1091#ifdef CONFIG_HOTPLUG_CPU
Dmitry Monakhove77b89f2009-10-05 00:38:55 +04001092 strncpy(per_cpu(cpufreq_cpu_governor, cpu), data->governor->name,
1093 CPUFREQ_NAME_LEN);
Thomas Renninger084f3492007-07-09 11:35:28 -07001094#endif
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 /* if we have other CPUs still registered, we need to unlink them,
1097 * or else wait_for_completion below will lock up. Clean the
Mike Travis7a6aedf2008-03-25 15:06:53 -07001098 * per_cpu(cpufreq_cpu_data) while holding the lock, and remove
1099 * the sysfs links afterwards.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 */
Rusty Russell835481d2009-01-04 05:18:06 -08001101 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1102 for_each_cpu(j, data->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (j == cpu)
1104 continue;
Mike Travis7a6aedf2008-03-25 15:06:53 -07001105 per_cpu(cpufreq_cpu_data, j) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
1107 }
1108
1109 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1110
Rusty Russell835481d2009-01-04 05:18:06 -08001111 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1112 for_each_cpu(j, data->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (j == cpu)
1114 continue;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001115 pr_debug("removing link for cpu %u\n", j);
Thomas Renninger084f3492007-07-09 11:35:28 -07001116#ifdef CONFIG_HOTPLUG_CPU
Dmitry Monakhove77b89f2009-10-05 00:38:55 +04001117 strncpy(per_cpu(cpufreq_cpu_governor, j),
1118 data->governor->name, CPUFREQ_NAME_LEN);
Thomas Renninger084f3492007-07-09 11:35:28 -07001119#endif
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001120 cpu_dev = get_cpu_device(j);
1121 kobj = &cpu_dev->kobj;
Amerigo Wang499bca92010-03-04 03:23:46 -05001122 unlock_policy_rwsem_write(cpu);
1123 sysfs_remove_link(kobj, "cpufreq");
1124 lock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 cpufreq_cpu_put(data);
1126 }
1127 }
1128#else
1129 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1130#endif
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 if (cpufreq_driver->target)
1133 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001134
Amerigo Wang499bca92010-03-04 03:23:46 -05001135 kobj = &data->kobj;
1136 cmp = &data->kobj_unregister;
1137 unlock_policy_rwsem_write(cpu);
1138 kobject_put(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 /* we need to make sure that the underlying kobj is actually
Dave Jones32ee8c32006-02-28 00:43:23 -05001141 * not referenced anymore by anybody before we proceed with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 * unloading.
1143 */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001144 pr_debug("waiting for dropping of refcount\n");
Amerigo Wang499bca92010-03-04 03:23:46 -05001145 wait_for_completion(cmp);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001146 pr_debug("wait complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Amerigo Wang499bca92010-03-04 03:23:46 -05001148 lock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 if (cpufreq_driver->exit)
1150 cpufreq_driver->exit(data);
venkatesh.pallipadi@intel.com7d26e2d2009-07-02 17:08:30 -07001151 unlock_policy_rwsem_write(cpu);
1152
Jacob Shin27ecddc2011-04-27 13:32:11 -05001153#ifdef CONFIG_HOTPLUG_CPU
1154 /* when the CPU which is the parent of the kobj is hotplugged
1155 * offline, check for siblings, and create cpufreq sysfs interface
1156 * and symlinks
1157 */
1158 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1159 /* first sibling now owns the new sysfs dir */
1160 cpumask_clear_cpu(cpu, data->cpus);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001161 cpufreq_add_dev(get_cpu_device(cpumask_first(data->cpus)), NULL);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001162
1163 /* finally remove our own symlink */
1164 lock_policy_rwsem_write(cpu);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001165 __cpufreq_remove_dev(dev, sif);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001166 }
1167#endif
1168
Rusty Russell835481d2009-01-04 05:18:06 -08001169 free_cpumask_var(data->related_cpus);
1170 free_cpumask_var(data->cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 kfree(data);
1172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 return 0;
1174}
1175
1176
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001177static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001178{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001179 unsigned int cpu = dev->id;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001180 int retval;
Venki Pallipadiec282972007-03-26 12:03:19 -07001181
1182 if (cpu_is_offline(cpu))
1183 return 0;
1184
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001185 if (unlikely(lock_policy_rwsem_write(cpu)))
1186 BUG();
1187
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001188 retval = __cpufreq_remove_dev(dev, sif);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001189 return retval;
1190}
1191
1192
David Howells65f27f32006-11-22 14:55:48 +00001193static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
David Howells65f27f32006-11-22 14:55:48 +00001195 struct cpufreq_policy *policy =
1196 container_of(work, struct cpufreq_policy, update);
1197 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001198 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 cpufreq_update_policy(cpu);
1200}
1201
1202/**
1203 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
1204 * @cpu: cpu number
1205 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1206 * @new_freq: CPU frequency the CPU actually runs at
1207 *
Dave Jones29464f22009-01-18 01:37:11 -05001208 * We adjust to current frequency first, and need to clean up later.
1209 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301211static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1212 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
1214 struct cpufreq_freqs freqs;
1215
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001216 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1218
1219 freqs.cpu = cpu;
1220 freqs.old = old_freq;
1221 freqs.new = new_freq;
1222 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
1223 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
1224}
1225
1226
Dave Jones32ee8c32006-02-28 00:43:23 -05001227/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301228 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001229 * @cpu: CPU number
1230 *
1231 * This is the last known freq, without actually getting it from the driver.
1232 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1233 */
1234unsigned int cpufreq_quick_get(unsigned int cpu)
1235{
1236 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301237 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001238
1239 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301240 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001241 cpufreq_cpu_put(policy);
1242 }
1243
Dave Jones4d34a672008-02-07 16:33:49 -05001244 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001245}
1246EXPORT_SYMBOL(cpufreq_quick_get);
1247
Jesse Barnes3d737102011-06-28 10:59:12 -07001248/**
1249 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1250 * @cpu: CPU number
1251 *
1252 * Just return the max possible frequency for a given CPU.
1253 */
1254unsigned int cpufreq_quick_get_max(unsigned int cpu)
1255{
1256 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1257 unsigned int ret_freq = 0;
1258
1259 if (policy) {
1260 ret_freq = policy->max;
1261 cpufreq_cpu_put(policy);
1262 }
1263
1264 return ret_freq;
1265}
1266EXPORT_SYMBOL(cpufreq_quick_get_max);
1267
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001268
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001269static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001271 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301272 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001275 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301277 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301279 if (ret_freq && policy->cur &&
1280 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1281 /* verify no discrepancy between actual and
1282 saved value exists */
1283 if (unlikely(ret_freq != policy->cur)) {
1284 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 schedule_work(&policy->update);
1286 }
1287 }
1288
Dave Jones4d34a672008-02-07 16:33:49 -05001289 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001290}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001292/**
1293 * cpufreq_get - get the current CPU frequency (in kHz)
1294 * @cpu: CPU number
1295 *
1296 * Get the CPU current (static) CPU frequency
1297 */
1298unsigned int cpufreq_get(unsigned int cpu)
1299{
1300 unsigned int ret_freq = 0;
1301 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1302
1303 if (!policy)
1304 goto out;
1305
1306 if (unlikely(lock_policy_rwsem_read(cpu)))
1307 goto out_policy;
1308
1309 ret_freq = __cpufreq_get(cpu);
1310
1311 unlock_policy_rwsem_read(cpu);
1312
1313out_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 cpufreq_cpu_put(policy);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001315out:
Dave Jones4d34a672008-02-07 16:33:49 -05001316 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317}
1318EXPORT_SYMBOL(cpufreq_get);
1319
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001320static struct subsys_interface cpufreq_interface = {
1321 .name = "cpufreq",
1322 .subsys = &cpu_subsys,
1323 .add_dev = cpufreq_add_dev,
1324 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001325};
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001329 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1330 *
1331 * This function is only executed for the boot processor. The other CPUs
1332 * have been put offline by means of CPU hotplug.
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001333 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001334static int cpufreq_bp_suspend(void)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001335{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301336 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001337
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001338 int cpu = smp_processor_id();
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001339 struct cpufreq_policy *cpu_policy;
1340
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001341 pr_debug("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001342
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001343 /* If there's no policy for the boot CPU, we have nothing to do. */
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001344 cpu_policy = cpufreq_cpu_get(cpu);
1345 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001346 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001347
1348 if (cpufreq_driver->suspend) {
Rafael J. Wysocki7ca64e22011-03-10 21:13:05 +01001349 ret = cpufreq_driver->suspend(cpu_policy);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001350 if (ret)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001351 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1352 "step on CPU %u\n", cpu_policy->cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001353 }
1354
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001355 cpufreq_cpu_put(cpu_policy);
Dave Jonesc9060492008-02-07 16:32:18 -05001356 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001357}
1358
1359/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001360 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 *
1362 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001363 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1364 * restored. It will verify that the current freq is in sync with
1365 * what we believe it to be. This is a bit later than when it
1366 * should be, but nonethteless it's better than calling
1367 * cpufreq_driver->get() here which might re-enable interrupts...
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001368 *
1369 * This function is only executed for the boot CPU. The other CPUs have not
1370 * been turned on yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001372static void cpufreq_bp_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301374 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001375
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001376 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 struct cpufreq_policy *cpu_policy;
1378
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001379 pr_debug("resuming cpu %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001381 /* If there's no policy for the boot CPU, we have nothing to do. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 cpu_policy = cpufreq_cpu_get(cpu);
1383 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001384 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 if (cpufreq_driver->resume) {
1387 ret = cpufreq_driver->resume(cpu_policy);
1388 if (ret) {
1389 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1390 "step on CPU %u\n", cpu_policy->cpu);
Dave Jonesc9060492008-02-07 16:32:18 -05001391 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 }
1393 }
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 schedule_work(&cpu_policy->update);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001396
Dave Jonesc9060492008-02-07 16:32:18 -05001397fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399}
1400
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001401static struct syscore_ops cpufreq_syscore_ops = {
1402 .suspend = cpufreq_bp_suspend,
1403 .resume = cpufreq_bp_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404};
1405
1406
1407/*********************************************************************
1408 * NOTIFIER LISTS INTERFACE *
1409 *********************************************************************/
1410
1411/**
1412 * cpufreq_register_notifier - register a driver with cpufreq
1413 * @nb: notifier function to register
1414 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1415 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001416 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 * are notified about clock rate changes (once before and once after
1418 * the transition), or a list of drivers that are notified about
1419 * changes in cpufreq policy.
1420 *
1421 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001422 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 */
1424int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1425{
1426 int ret;
1427
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001428 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 switch (list) {
1431 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001432 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001433 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 break;
1435 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001436 ret = blocking_notifier_chain_register(
1437 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 break;
1439 default:
1440 ret = -EINVAL;
1441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 return ret;
1444}
1445EXPORT_SYMBOL(cpufreq_register_notifier);
1446
1447
1448/**
1449 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1450 * @nb: notifier block to be unregistered
1451 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1452 *
1453 * Remove a driver from the CPU frequency notifier list.
1454 *
1455 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001456 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 */
1458int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1459{
1460 int ret;
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 switch (list) {
1463 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001464 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001465 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 break;
1467 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001468 ret = blocking_notifier_chain_unregister(
1469 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 break;
1471 default:
1472 ret = -EINVAL;
1473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
1475 return ret;
1476}
1477EXPORT_SYMBOL(cpufreq_unregister_notifier);
1478
1479
1480/*********************************************************************
1481 * GOVERNORS *
1482 *********************************************************************/
1483
1484
1485int __cpufreq_driver_target(struct cpufreq_policy *policy,
1486 unsigned int target_freq,
1487 unsigned int relation)
1488{
1489 int retval = -EINVAL;
Viresh Kumar72499242012-10-31 01:28:21 +01001490 unsigned int old_target_freq = target_freq;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001491
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001492 if (cpufreq_disabled())
1493 return -ENODEV;
1494
Viresh Kumar72499242012-10-31 01:28:21 +01001495 /* Make sure that target_freq is within supported range */
1496 if (target_freq > policy->max)
1497 target_freq = policy->max;
1498 if (target_freq < policy->min)
1499 target_freq = policy->min;
1500
1501 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1502 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001503
1504 if (target_freq == policy->cur)
1505 return 0;
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1508 retval = cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 return retval;
1511}
1512EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514int cpufreq_driver_target(struct cpufreq_policy *policy,
1515 unsigned int target_freq,
1516 unsigned int relation)
1517{
Julia Lawallf1829e42008-07-25 22:44:53 +02001518 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520 policy = cpufreq_cpu_get(policy->cpu);
1521 if (!policy)
Julia Lawallf1829e42008-07-25 22:44:53 +02001522 goto no_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001524 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
Julia Lawallf1829e42008-07-25 22:44:53 +02001525 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 ret = __cpufreq_driver_target(policy, target_freq, relation);
1528
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001529 unlock_policy_rwsem_write(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Julia Lawallf1829e42008-07-25 22:44:53 +02001531fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 cpufreq_cpu_put(policy);
Julia Lawallf1829e42008-07-25 22:44:53 +02001533no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 return ret;
1535}
1536EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1537
venkatesh.pallipadi@intel.combf0b90e2008-08-04 11:59:07 -07001538int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu)
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001539{
1540 int ret = 0;
1541
Viresh Kumar0676f7f2012-10-24 23:39:48 +02001542 if (!(cpu_online(cpu) && cpufreq_driver->getavg))
1543 return 0;
1544
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001545 policy = cpufreq_cpu_get(policy->cpu);
1546 if (!policy)
1547 return -EINVAL;
1548
Viresh Kumar0676f7f2012-10-24 23:39:48 +02001549 ret = cpufreq_driver->getavg(policy, cpu);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001550
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001551 cpufreq_cpu_put(policy);
1552 return ret;
1553}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001554EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001555
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001556/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001557 * when "event" is CPUFREQ_GOV_LIMITS
1558 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301560static int __cpufreq_governor(struct cpufreq_policy *policy,
1561 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
Dave Jonescc993ca2005-07-28 09:43:56 -07001563 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001564
1565 /* Only must be defined when default governor is known to have latency
1566 restrictions, like e.g. conservative or ondemand.
1567 That this is the case is already ensured in Kconfig
1568 */
1569#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1570 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1571#else
1572 struct cpufreq_governor *gov = NULL;
1573#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001574
1575 if (policy->governor->max_transition_latency &&
1576 policy->cpuinfo.transition_latency >
1577 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07001578 if (!gov)
1579 return -EINVAL;
1580 else {
1581 printk(KERN_WARNING "%s governor failed, too long"
1582 " transition latency of HW, fallback"
1583 " to %s governor\n",
1584 policy->governor->name,
1585 gov->name);
1586 policy->governor = gov;
1587 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
1590 if (!try_module_get(policy->governor->owner))
1591 return -EINVAL;
1592
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001593 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301594 policy->cpu, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 ret = policy->governor->governor(policy, event);
1596
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301597 /* we keep one module reference alive for
1598 each CPU governed by this CPU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 if ((event != CPUFREQ_GOV_START) || ret)
1600 module_put(policy->governor->owner);
1601 if ((event == CPUFREQ_GOV_STOP) && !ret)
1602 module_put(policy->governor->owner);
1603
1604 return ret;
1605}
1606
1607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608int cpufreq_register_governor(struct cpufreq_governor *governor)
1609{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001610 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
1612 if (!governor)
1613 return -EINVAL;
1614
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001615 if (cpufreq_disabled())
1616 return -ENODEV;
1617
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001618 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001619
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001620 err = -EBUSY;
1621 if (__find_governor(governor->name) == NULL) {
1622 err = 0;
1623 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Dave Jones32ee8c32006-02-28 00:43:23 -05001626 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001627 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628}
1629EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1630
1631
1632void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1633{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001634#ifdef CONFIG_HOTPLUG_CPU
1635 int cpu;
1636#endif
1637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 if (!governor)
1639 return;
1640
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001641 if (cpufreq_disabled())
1642 return;
1643
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001644#ifdef CONFIG_HOTPLUG_CPU
1645 for_each_present_cpu(cpu) {
1646 if (cpu_online(cpu))
1647 continue;
1648 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1649 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1650 }
1651#endif
1652
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001653 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001655 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 return;
1657}
1658EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1659
1660
1661
1662/*********************************************************************
1663 * POLICY INTERFACE *
1664 *********************************************************************/
1665
1666/**
1667 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05001668 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1669 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 *
1671 * Reads the current cpufreq policy.
1672 */
1673int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1674{
1675 struct cpufreq_policy *cpu_policy;
1676 if (!policy)
1677 return -EINVAL;
1678
1679 cpu_policy = cpufreq_cpu_get(cpu);
1680 if (!cpu_policy)
1681 return -EINVAL;
1682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 return 0;
1687}
1688EXPORT_SYMBOL(cpufreq_get_policy);
1689
1690
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001691/*
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301692 * data : current policy.
1693 * policy : policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001694 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301695static int __cpufreq_set_policy(struct cpufreq_policy *data,
1696 struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
1698 int ret = 0;
1699
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001700 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 policy->min, policy->max);
1702
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301703 memcpy(&policy->cpuinfo, &data->cpuinfo,
1704 sizeof(struct cpufreq_cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
Yi Yang53391fa2008-01-30 13:33:34 +01001706 if (policy->min > data->max || policy->max < data->min) {
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02001707 ret = -EINVAL;
1708 goto error_out;
1709 }
1710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 /* verify the cpu speed can be set within this limit */
1712 ret = cpufreq_driver->verify(policy);
1713 if (ret)
1714 goto error_out;
1715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001717 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1718 CPUFREQ_ADJUST, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001721 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1722 CPUFREQ_INCOMPATIBLE, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724 /* verify the cpu speed can be set within this limit,
1725 which might be different to the first one */
1726 ret = cpufreq_driver->verify(policy);
Alan Sterne041c682006-03-27 01:16:30 -08001727 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
1730 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001731 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1732 CPUFREQ_NOTIFY, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Dave Jones7d5e3502006-02-02 17:03:42 -05001734 data->min = policy->min;
1735 data->max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001737 pr_debug("new min and max freqs are %u - %u kHz\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301738 data->min, data->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
1740 if (cpufreq_driver->setpolicy) {
1741 data->policy = policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001742 pr_debug("setting range\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 ret = cpufreq_driver->setpolicy(policy);
1744 } else {
1745 if (policy->governor != data->governor) {
1746 /* save old, working values */
1747 struct cpufreq_governor *old_gov = data->governor;
1748
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001749 pr_debug("governor switch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751 /* end old governor */
Andrej Gelenbergffe62752010-05-14 15:15:58 -07001752 if (data->governor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1754
1755 /* start new governor */
1756 data->governor = policy->governor;
1757 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1758 /* new governor failed, so re-start old one */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001759 pr_debug("starting governor %s failed\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301760 data->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 if (old_gov) {
1762 data->governor = old_gov;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301763 __cpufreq_governor(data,
1764 CPUFREQ_GOV_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 }
1766 ret = -EINVAL;
1767 goto error_out;
1768 }
1769 /* might be a policy change, too, so fall through */
1770 }
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001771 pr_debug("governor: change or update limits\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1773 }
1774
Dave Jones7d5e3502006-02-02 17:03:42 -05001775error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 return ret;
1777}
1778
1779/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1781 * @cpu: CPU which shall be re-evaluated
1782 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001783 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 * at different times.
1785 */
1786int cpufreq_update_policy(unsigned int cpu)
1787{
1788 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1789 struct cpufreq_policy policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02001790 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Julia Lawallf1829e42008-07-25 22:44:53 +02001792 if (!data) {
1793 ret = -ENODEV;
1794 goto no_policy;
1795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Julia Lawallf1829e42008-07-25 22:44:53 +02001797 if (unlikely(lock_policy_rwsem_write(cpu))) {
1798 ret = -EINVAL;
1799 goto fail;
1800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001802 pr_debug("updating policy for CPU %u\n", cpu);
Dave Jones7d5e3502006-02-02 17:03:42 -05001803 memcpy(&policy, data, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 policy.min = data->user_policy.min;
1805 policy.max = data->user_policy.max;
1806 policy.policy = data->user_policy.policy;
1807 policy.governor = data->user_policy.governor;
1808
Thomas Renninger0961dd02006-01-26 18:46:33 +01001809 /* BIOS might change freq behind our back
1810 -> ask driver for current freq and notify governors about a change */
1811 if (cpufreq_driver->get) {
1812 policy.cur = cpufreq_driver->get(cpu);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001813 if (!data->cur) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001814 pr_debug("Driver did not initialize current freq");
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001815 data->cur = policy.cur;
1816 } else {
1817 if (data->cur != policy.cur)
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301818 cpufreq_out_of_sync(cpu, data->cur,
1819 policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001820 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01001821 }
1822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 ret = __cpufreq_set_policy(data, &policy);
1824
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001825 unlock_policy_rwsem_write(cpu);
1826
Julia Lawallf1829e42008-07-25 22:44:53 +02001827fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 cpufreq_cpu_put(data);
Julia Lawallf1829e42008-07-25 22:44:53 +02001829no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 return ret;
1831}
1832EXPORT_SYMBOL(cpufreq_update_policy);
1833
Satyam Sharmadd184a02007-10-02 13:28:14 -07001834static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08001835 unsigned long action, void *hcpu)
1836{
1837 unsigned int cpu = (unsigned long)hcpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001838 struct device *dev;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001839
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001840 dev = get_cpu_device(cpu);
1841 if (dev) {
Ashok Rajc32b6b82005-10-30 14:59:54 -08001842 switch (action) {
1843 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001844 case CPU_ONLINE_FROZEN:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001845 cpufreq_add_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001846 break;
1847 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001848 case CPU_DOWN_PREPARE_FROZEN:
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001849 if (unlikely(lock_policy_rwsem_write(cpu)))
1850 BUG();
1851
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001852 __cpufreq_remove_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001853 break;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001854 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001855 case CPU_DOWN_FAILED_FROZEN:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001856 cpufreq_add_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001857 break;
1858 }
1859 }
1860 return NOTIFY_OK;
1861}
1862
Neal Buckendahl9c36f742010-06-22 22:02:44 -05001863static struct notifier_block __refdata cpufreq_cpu_notifier = {
Ashok Rajc32b6b82005-10-30 14:59:54 -08001864 .notifier_call = cpufreq_cpu_callback,
1865};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
1867/*********************************************************************
1868 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1869 *********************************************************************/
1870
1871/**
1872 * cpufreq_register_driver - register a CPU Frequency driver
1873 * @driver_data: A struct cpufreq_driver containing the values#
1874 * submitted by the CPU Frequency driver.
1875 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001876 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05001878 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 *
1880 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001881int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882{
1883 unsigned long flags;
1884 int ret;
1885
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001886 if (cpufreq_disabled())
1887 return -ENODEV;
1888
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 if (!driver_data || !driver_data->verify || !driver_data->init ||
1890 ((!driver_data->setpolicy) && (!driver_data->target)))
1891 return -EINVAL;
1892
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001893 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
1895 if (driver_data->setpolicy)
1896 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1897
1898 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1899 if (cpufreq_driver) {
1900 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1901 return -EBUSY;
1902 }
1903 cpufreq_driver = driver_data;
1904 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1905
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001906 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001907 if (ret)
1908 goto err_null_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001910 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 int i;
1912 ret = -ENODEV;
1913
1914 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07001915 for (i = 0; i < nr_cpu_ids; i++)
1916 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07001918 break;
1919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
1921 /* if all ->init() calls failed, unregister */
1922 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001923 pr_debug("no CPU initialized for driver %s\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301924 driver_data->name);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001925 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 }
1927 }
1928
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001929 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001930 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001932 return 0;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001933err_if_unreg:
1934 subsys_interface_unregister(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001935err_null_driver:
1936 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1937 cpufreq_driver = NULL;
1938 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05001939 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940}
1941EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1942
1943
1944/**
1945 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1946 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001947 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 * the right to do so, i.e. if you have succeeded in initialising before!
1949 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1950 * currently not initialised.
1951 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001952int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
1954 unsigned long flags;
1955
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001956 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001959 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001961 subsys_interface_unregister(&cpufreq_interface);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001962 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
1964 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1965 cpufreq_driver = NULL;
1966 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1967
1968 return 0;
1969}
1970EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001971
1972static int __init cpufreq_core_init(void)
1973{
1974 int cpu;
1975
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001976 if (cpufreq_disabled())
1977 return -ENODEV;
1978
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001979 for_each_possible_cpu(cpu) {
Tejun Heof1625062009-10-29 22:34:13 +09001980 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001981 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
1982 }
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02001983
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001984 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02001985 BUG_ON(!cpufreq_global_kobject);
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001986 register_syscore_ops(&cpufreq_syscore_ops);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02001987
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001988 return 0;
1989}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001990core_initcall(cpufreq_core_init);