blob: 034d1836884bd4d56a9e4ad640d3e84a8a82b166 [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
Viresh Kumar6954ca92013-01-12 05:14:40 +000050/* Used when we unregister cpufreq driver */
51static struct cpumask cpufreq_online_mask;
52
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080053/*
54 * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
55 * all cpufreq/hotplug/workqueue/etc related lock issues.
56 *
57 * The rules for this semaphore:
58 * - Any routine that wants to read from the policy structure will
59 * do a down_read on this semaphore.
60 * - Any routine that will write to the policy structure and/or may take away
61 * the policy altogether (eg. CPU hotplug), will hold this lock in write
62 * mode before doing so.
63 *
64 * Additional rules:
65 * - All holders of the lock should check to make sure that the CPU they
66 * are concerned with are online after they get the lock.
67 * - Governor routines that can be called in cpufreq hotplug path should not
68 * take this sem as top level hotplug notifier handler takes this.
Mathieu Desnoyers395913d2009-06-08 13:17:31 -040069 * - Lock should not be held across
70 * __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080071 */
Tejun Heof1625062009-10-29 22:34:13 +090072static DEFINE_PER_CPU(int, cpufreq_policy_cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080073static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
74
75#define lock_policy_rwsem(mode, cpu) \
Amerigo Wang226528c2010-03-04 03:23:36 -050076static int lock_policy_rwsem_##mode \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080077(int cpu) \
78{ \
Tejun Heof1625062009-10-29 22:34:13 +090079 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080080 BUG_ON(policy_cpu == -1); \
81 down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
82 if (unlikely(!cpu_online(cpu))) { \
83 up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
84 return -1; \
85 } \
86 \
87 return 0; \
88}
89
90lock_policy_rwsem(read, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080091
92lock_policy_rwsem(write, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080093
Amerigo Wang226528c2010-03-04 03:23:36 -050094static void unlock_policy_rwsem_read(int cpu)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080095{
Tejun Heof1625062009-10-29 22:34:13 +090096 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080097 BUG_ON(policy_cpu == -1);
98 up_read(&per_cpu(cpu_policy_rwsem, policy_cpu));
99}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800100
Amerigo Wang226528c2010-03-04 03:23:36 -0500101static void unlock_policy_rwsem_write(int cpu)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800102{
Tejun Heof1625062009-10-29 22:34:13 +0900103 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800104 BUG_ON(policy_cpu == -1);
105 up_write(&per_cpu(cpu_policy_rwsem, policy_cpu));
106}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800107
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/* internal prototypes */
Dave Jones29464f22009-01-18 01:37:11 -0500110static int __cpufreq_governor(struct cpufreq_policy *policy,
111 unsigned int event);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800112static unsigned int __cpufreq_get(unsigned int cpu);
David Howells65f27f32006-11-22 14:55:48 +0000113static void handle_update(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115/**
Dave Jones32ee8c32006-02-28 00:43:23 -0500116 * Two notifier lists: the "policy" list is involved in the
117 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 * "transition" list for kernel code that needs to handle
119 * changes to devices when the CPU clock speed changes.
120 * The mutex locks both lists.
121 */
Alan Sterne041c682006-03-27 01:16:30 -0800122static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700123static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200125static bool init_cpufreq_transition_notifier_list_called;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700126static int __init init_cpufreq_transition_notifier_list(void)
127{
128 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200129 init_cpufreq_transition_notifier_list_called = true;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700130 return 0;
131}
Linus Torvaldsb3438f82006-11-20 11:47:18 -0800132pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400134static int off __read_mostly;
Viresh Kumarda584452012-10-26 00:51:32 +0200135static int cpufreq_disabled(void)
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400136{
137 return off;
138}
139void disable_cpufreq(void)
140{
141 off = 1;
142}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143static LIST_HEAD(cpufreq_governor_list);
Dave Jones29464f22009-01-18 01:37:11 -0500144static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Stephen Boyda9144432012-07-20 18:14:38 +0000146static struct cpufreq_policy *__cpufreq_cpu_get(unsigned int cpu, bool sysfs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 struct cpufreq_policy *data;
149 unsigned long flags;
150
Mike Travis7a6aedf2008-03-25 15:06:53 -0700151 if (cpu >= nr_cpu_ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 goto err_out;
153
154 /* get the cpufreq driver */
155 spin_lock_irqsave(&cpufreq_driver_lock, flags);
156
157 if (!cpufreq_driver)
158 goto err_out_unlock;
159
160 if (!try_module_get(cpufreq_driver->owner))
161 goto err_out_unlock;
162
163
164 /* get the CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -0700165 data = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 if (!data)
168 goto err_out_put_module;
169
Stephen Boyda9144432012-07-20 18:14:38 +0000170 if (!sysfs && !kobject_get(&data->kobj))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 goto err_out_put_module;
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return data;
175
Dave Jones7d5e3502006-02-02 17:03:42 -0500176err_out_put_module:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 module_put(cpufreq_driver->owner);
Dave Jones7d5e3502006-02-02 17:03:42 -0500178err_out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones7d5e3502006-02-02 17:03:42 -0500180err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return NULL;
182}
Stephen Boyda9144432012-07-20 18:14:38 +0000183
184struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
185{
186 return __cpufreq_cpu_get(cpu, false);
187}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
189
Stephen Boyda9144432012-07-20 18:14:38 +0000190static struct cpufreq_policy *cpufreq_cpu_get_sysfs(unsigned int cpu)
191{
192 return __cpufreq_cpu_get(cpu, true);
193}
194
195static void __cpufreq_cpu_put(struct cpufreq_policy *data, bool sysfs)
196{
197 if (!sysfs)
198 kobject_put(&data->kobj);
199 module_put(cpufreq_driver->owner);
200}
Dave Jones7d5e3502006-02-02 17:03:42 -0500201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202void cpufreq_cpu_put(struct cpufreq_policy *data)
203{
Stephen Boyda9144432012-07-20 18:14:38 +0000204 __cpufreq_cpu_put(data, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
207
Stephen Boyda9144432012-07-20 18:14:38 +0000208static void cpufreq_cpu_put_sysfs(struct cpufreq_policy *data)
209{
210 __cpufreq_cpu_put(data, true);
211}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
215 *********************************************************************/
216
217/**
218 * adjust_jiffies - adjust the system "loops_per_jiffy"
219 *
220 * This function alters the system "loops_per_jiffy" for the clock
221 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500222 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 * per-CPU loops_per_jiffy value wherever possible.
224 */
225#ifndef CONFIG_SMP
226static unsigned long l_p_j_ref;
227static unsigned int l_p_j_ref_freq;
228
Arjan van de Ven858119e2006-01-14 13:20:43 -0800229static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 if (ci->flags & CPUFREQ_CONST_LOOPS)
232 return;
233
234 if (!l_p_j_ref_freq) {
235 l_p_j_ref = loops_per_jiffy;
236 l_p_j_ref_freq = ci->old;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200237 pr_debug("saving %lu as reference value for loops_per_jiffy; "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530238 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
Afzal Mohammedd08de0c12012-01-04 10:52:46 +0530240 if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700241 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530242 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
243 ci->new);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200244 pr_debug("scaling loops_per_jiffy to %lu "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530245 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
247}
248#else
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530249static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
250{
251 return;
252}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253#endif
254
255
256/**
Dave Jonese4472cb2006-01-31 15:53:55 -0800257 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
258 * on frequency transition.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 *
Dave Jonese4472cb2006-01-31 15:53:55 -0800260 * This function calls the transition notifiers and the "adjust_jiffies"
261 * function. It is called twice on all CPU frequency changes that have
Dave Jones32ee8c32006-02-28 00:43:23 -0500262 * external effects.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 */
264void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
265{
Dave Jonese4472cb2006-01-31 15:53:55 -0800266 struct cpufreq_policy *policy;
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 BUG_ON(irqs_disabled());
269
270 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200271 pr_debug("notification %u of frequency transition to %u kHz\n",
Dave Jonese4472cb2006-01-31 15:53:55 -0800272 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Mike Travis7a6aedf2008-03-25 15:06:53 -0700274 policy = per_cpu(cpufreq_cpu_data, freqs->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500278 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800279 * which is not equal to what the cpufreq core thinks is
280 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 */
282 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800283 if ((policy) && (policy->cpu == freqs->cpu) &&
284 (policy->cur) && (policy->cur != freqs->old)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200285 pr_debug("Warning: CPU frequency is"
Dave Jonese4472cb2006-01-31 15:53:55 -0800286 " %u, cpufreq assumed %u kHz.\n",
287 freqs->old, policy->cur);
288 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700291 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800292 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
294 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 case CPUFREQ_POSTCHANGE:
297 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200298 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
Thomas Renninger6f4f2722010-04-20 13:17:36 +0200299 (unsigned long)freqs->cpu);
300 trace_power_frequency(POWER_PSTATE, freqs->new, freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100301 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700302 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800303 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800304 if (likely(policy) && likely(policy->cpu == freqs->cpu))
305 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 break;
307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
310
311
312
313/*********************************************************************
314 * SYSFS INTERFACE *
315 *********************************************************************/
316
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700317static struct cpufreq_governor *__find_governor(const char *str_governor)
318{
319 struct cpufreq_governor *t;
320
321 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
Dave Jones29464f22009-01-18 01:37:11 -0500322 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700323 return t;
324
325 return NULL;
326}
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328/**
329 * cpufreq_parse_governor - parse a governor string
330 */
Dave Jones905d77c2008-03-05 14:28:32 -0500331static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 struct cpufreq_governor **governor)
333{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700334 int err = -EINVAL;
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700337 goto out;
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (cpufreq_driver->setpolicy) {
340 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
341 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700342 err = 0;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530343 } else if (!strnicmp(str_governor, "powersave",
344 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700346 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700348 } else if (cpufreq_driver->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700350
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800351 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700352
353 t = __find_governor(str_governor);
354
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700355 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700356 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700357
Kees Cook1a8e1462011-05-04 08:38:56 -0700358 mutex_unlock(&cpufreq_governor_mutex);
359 ret = request_module("cpufreq_%s", str_governor);
360 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700361
Kees Cook1a8e1462011-05-04 08:38:56 -0700362 if (ret == 0)
363 t = __find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700364 }
365
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700366 if (t != NULL) {
367 *governor = t;
368 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700370
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800371 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
Dave Jones29464f22009-01-18 01:37:11 -0500373out:
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700374 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530379 * cpufreq_per_cpu_attr_read() / show_##file_name() -
380 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 *
382 * Write out information from cpufreq_driver->policy[cpu]; object must be
383 * "unsigned int".
384 */
385
Dave Jones32ee8c32006-02-28 00:43:23 -0500386#define show_one(file_name, object) \
387static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500388(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500389{ \
Dave Jones29464f22009-01-18 01:37:11 -0500390 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
393show_one(cpuinfo_min_freq, cpuinfo.min_freq);
394show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100395show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396show_one(scaling_min_freq, min);
397show_one(scaling_max_freq, max);
398show_one(scaling_cur_freq, cur);
399
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530400static int __cpufreq_set_policy(struct cpufreq_policy *data,
401 struct cpufreq_policy *policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403/**
404 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
405 */
406#define store_one(file_name, object) \
407static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500408(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{ \
Jingoo Hanf55c9c22012-10-31 05:49:13 +0000410 unsigned int ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 struct cpufreq_policy new_policy; \
412 \
413 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
414 if (ret) \
415 return -EINVAL; \
416 \
Dave Jones29464f22009-01-18 01:37:11 -0500417 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (ret != 1) \
419 return -EINVAL; \
420 \
Thomas Renninger7970e082006-04-13 15:14:04 +0200421 ret = __cpufreq_set_policy(policy, &new_policy); \
422 policy->user_policy.object = policy->object; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 \
424 return ret ? ret : count; \
425}
426
Dave Jones29464f22009-01-18 01:37:11 -0500427store_one(scaling_min_freq, min);
428store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430/**
431 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
432 */
Dave Jones905d77c2008-03-05 14:28:32 -0500433static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
434 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800436 unsigned int cur_freq = __cpufreq_get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (!cur_freq)
438 return sprintf(buf, "<unknown>");
439 return sprintf(buf, "%u\n", cur_freq);
440}
441
442
443/**
444 * show_scaling_governor - show the current policy for the specified CPU
445 */
Dave Jones905d77c2008-03-05 14:28:32 -0500446static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Dave Jones29464f22009-01-18 01:37:11 -0500448 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return sprintf(buf, "powersave\n");
450 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
451 return sprintf(buf, "performance\n");
452 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200453 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500454 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return -EINVAL;
456}
457
458
459/**
460 * store_scaling_governor - store policy for the specified CPU
461 */
Dave Jones905d77c2008-03-05 14:28:32 -0500462static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
463 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Jingoo Hanf55c9c22012-10-31 05:49:13 +0000465 unsigned int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 char str_governor[16];
467 struct cpufreq_policy new_policy;
468
469 ret = cpufreq_get_policy(&new_policy, policy->cpu);
470 if (ret)
471 return ret;
472
Dave Jones29464f22009-01-18 01:37:11 -0500473 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (ret != 1)
475 return -EINVAL;
476
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530477 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
478 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return -EINVAL;
480
Thomas Renninger7970e082006-04-13 15:14:04 +0200481 /* Do not use cpufreq_set_policy here or the user_policy.max
482 will be wrongly overridden */
Thomas Renninger7970e082006-04-13 15:14:04 +0200483 ret = __cpufreq_set_policy(policy, &new_policy);
484
485 policy->user_policy.policy = policy->policy;
486 policy->user_policy.governor = policy->governor;
Thomas Renninger7970e082006-04-13 15:14:04 +0200487
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530488 if (ret)
489 return ret;
490 else
491 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
494/**
495 * show_scaling_driver - show the cpufreq driver currently loaded
496 */
Dave Jones905d77c2008-03-05 14:28:32 -0500497static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
viresh kumar4b972f02012-10-23 01:23:43 +0200499 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
502/**
503 * show_scaling_available_governors - show the available CPUfreq governors
504 */
Dave Jones905d77c2008-03-05 14:28:32 -0500505static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
506 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
508 ssize_t i = 0;
509 struct cpufreq_governor *t;
510
511 if (!cpufreq_driver->target) {
512 i += sprintf(buf, "performance powersave");
513 goto out;
514 }
515
516 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
Dave Jones29464f22009-01-18 01:37:11 -0500517 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
518 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200520 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500522out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 i += sprintf(&buf[i], "\n");
524 return i;
525}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700526
Rusty Russell835481d2009-01-04 05:18:06 -0800527static ssize_t show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 ssize_t i = 0;
530 unsigned int cpu;
531
Rusty Russell835481d2009-01-04 05:18:06 -0800532 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (i)
534 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
535 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
536 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500537 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539 i += sprintf(&buf[i], "\n");
540 return i;
541}
542
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700543/**
544 * show_related_cpus - show the CPUs affected by each transition even if
545 * hw coordination is in use
546 */
547static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
548{
Rusty Russell835481d2009-01-04 05:18:06 -0800549 if (cpumask_empty(policy->related_cpus))
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700550 return show_cpus(policy->cpus, buf);
551 return show_cpus(policy->related_cpus, buf);
552}
553
554/**
555 * show_affected_cpus - show the CPUs affected by each transition
556 */
557static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
558{
559 return show_cpus(policy->cpus, buf);
560}
561
Venki Pallipadi9e769882007-10-26 10:18:21 -0700562static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500563 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700564{
565 unsigned int freq = 0;
566 unsigned int ret;
567
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700568 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700569 return -EINVAL;
570
571 ret = sscanf(buf, "%u", &freq);
572 if (ret != 1)
573 return -EINVAL;
574
575 policy->governor->store_setspeed(policy, freq);
576
577 return count;
578}
579
580static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
581{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700582 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700583 return sprintf(buf, "<unsupported>\n");
584
585 return policy->governor->show_setspeed(policy, buf);
586}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Thomas Renningere2f74f32009-11-19 12:31:01 +0100588/**
viresh kumar8bf1ac722012-10-23 01:23:33 +0200589 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100590 */
591static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
592{
593 unsigned int limit;
594 int ret;
595 if (cpufreq_driver->bios_limit) {
596 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
597 if (!ret)
598 return sprintf(buf, "%u\n", limit);
599 }
600 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
601}
602
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200603cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
604cpufreq_freq_attr_ro(cpuinfo_min_freq);
605cpufreq_freq_attr_ro(cpuinfo_max_freq);
606cpufreq_freq_attr_ro(cpuinfo_transition_latency);
607cpufreq_freq_attr_ro(scaling_available_governors);
608cpufreq_freq_attr_ro(scaling_driver);
609cpufreq_freq_attr_ro(scaling_cur_freq);
610cpufreq_freq_attr_ro(bios_limit);
611cpufreq_freq_attr_ro(related_cpus);
612cpufreq_freq_attr_ro(affected_cpus);
613cpufreq_freq_attr_rw(scaling_min_freq);
614cpufreq_freq_attr_rw(scaling_max_freq);
615cpufreq_freq_attr_rw(scaling_governor);
616cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Dave Jones905d77c2008-03-05 14:28:32 -0500618static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 &cpuinfo_min_freq.attr,
620 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100621 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 &scaling_min_freq.attr,
623 &scaling_max_freq.attr,
624 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700625 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 &scaling_governor.attr,
627 &scaling_driver.attr,
628 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700629 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 NULL
631};
632
Thomas Renninger8aa84ad2009-07-24 15:25:05 +0200633struct kobject *cpufreq_global_kobject;
634EXPORT_SYMBOL(cpufreq_global_kobject);
635
Dave Jones29464f22009-01-18 01:37:11 -0500636#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
637#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Dave Jones29464f22009-01-18 01:37:11 -0500639static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Dave Jones905d77c2008-03-05 14:28:32 -0500641 struct cpufreq_policy *policy = to_policy(kobj);
642 struct freq_attr *fattr = to_attr(attr);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500643 ssize_t ret = -EINVAL;
Stephen Boyda9144432012-07-20 18:14:38 +0000644 policy = cpufreq_cpu_get_sysfs(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (!policy)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500646 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800647
648 if (lock_policy_rwsem_read(policy->cpu) < 0)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500649 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800650
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530651 if (fattr->show)
652 ret = fattr->show(policy, buf);
653 else
654 ret = -EIO;
655
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800656 unlock_policy_rwsem_read(policy->cpu);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500657fail:
Stephen Boyda9144432012-07-20 18:14:38 +0000658 cpufreq_cpu_put_sysfs(policy);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500659no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return ret;
661}
662
Dave Jones905d77c2008-03-05 14:28:32 -0500663static ssize_t store(struct kobject *kobj, struct attribute *attr,
664 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Dave Jones905d77c2008-03-05 14:28:32 -0500666 struct cpufreq_policy *policy = to_policy(kobj);
667 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500668 ssize_t ret = -EINVAL;
Stephen Boyda9144432012-07-20 18:14:38 +0000669 policy = cpufreq_cpu_get_sysfs(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (!policy)
Dave Jonesa07530b2008-03-05 14:22:25 -0500671 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800672
673 if (lock_policy_rwsem_write(policy->cpu) < 0)
Dave Jonesa07530b2008-03-05 14:22:25 -0500674 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800675
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530676 if (fattr->store)
677 ret = fattr->store(policy, buf, count);
678 else
679 ret = -EIO;
680
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800681 unlock_policy_rwsem_write(policy->cpu);
Dave Jonesa07530b2008-03-05 14:22:25 -0500682fail:
Stephen Boyda9144432012-07-20 18:14:38 +0000683 cpufreq_cpu_put_sysfs(policy);
Dave Jonesa07530b2008-03-05 14:22:25 -0500684no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return ret;
686}
687
Dave Jones905d77c2008-03-05 14:28:32 -0500688static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Dave Jones905d77c2008-03-05 14:28:32 -0500690 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200691 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 complete(&policy->kobj_unregister);
693}
694
Emese Revfy52cf25d2010-01-19 02:58:23 +0100695static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 .show = show,
697 .store = store,
698};
699
700static struct kobj_type ktype_cpufreq = {
701 .sysfs_ops = &sysfs_ops,
702 .default_attrs = default_attrs,
703 .release = cpufreq_sysfs_release,
704};
705
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200706/*
707 * Returns:
708 * Negative: Failure
709 * 0: Success
710 * Positive: When we have a managed CPU and the sysfs got symlinked
711 */
Alex Chiangcf3289d02009-11-17 20:27:08 -0700712static int cpufreq_add_dev_policy(unsigned int cpu,
713 struct cpufreq_policy *policy,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800714 struct device *dev)
Dave Jonesecf7e462009-07-08 18:48:47 -0400715{
716 int ret = 0;
717#ifdef CONFIG_SMP
718 unsigned long flags;
719 unsigned int j;
Dave Jonesecf7e462009-07-08 18:48:47 -0400720#ifdef CONFIG_HOTPLUG_CPU
Dmitry Monakhove77b89f2009-10-05 00:38:55 +0400721 struct cpufreq_governor *gov;
722
723 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
724 if (gov) {
725 policy->governor = gov;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200726 pr_debug("Restoring governor %s for cpu %d\n",
Dave Jonesecf7e462009-07-08 18:48:47 -0400727 policy->governor->name, cpu);
728 }
729#endif
730
731 for_each_cpu(j, policy->cpus) {
732 struct cpufreq_policy *managed_policy;
733
734 if (cpu == j)
735 continue;
736
737 /* Check for existing affected CPUs.
738 * They may not be aware of it due to CPU Hotplug.
739 * cpufreq_cpu_put is called when the device is removed
740 * in __cpufreq_remove_dev()
741 */
742 managed_policy = cpufreq_cpu_get(j);
743 if (unlikely(managed_policy)) {
744
745 /* Set proper policy_cpu */
746 unlock_policy_rwsem_write(cpu);
Tejun Heof1625062009-10-29 22:34:13 +0900747 per_cpu(cpufreq_policy_cpu, cpu) = managed_policy->cpu;
Dave Jonesecf7e462009-07-08 18:48:47 -0400748
749 if (lock_policy_rwsem_write(cpu) < 0) {
750 /* Should not go through policy unlock path */
751 if (cpufreq_driver->exit)
752 cpufreq_driver->exit(policy);
753 cpufreq_cpu_put(managed_policy);
754 return -EBUSY;
755 }
756
Viresh Kumarf6a74092013-01-12 05:14:39 +0000757 __cpufreq_governor(managed_policy, CPUFREQ_GOV_STOP);
758
Dave Jonesecf7e462009-07-08 18:48:47 -0400759 spin_lock_irqsave(&cpufreq_driver_lock, flags);
760 cpumask_copy(managed_policy->cpus, policy->cpus);
761 per_cpu(cpufreq_cpu_data, cpu) = managed_policy;
762 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
763
Viresh Kumarf6a74092013-01-12 05:14:39 +0000764 __cpufreq_governor(managed_policy, CPUFREQ_GOV_START);
765 __cpufreq_governor(managed_policy, CPUFREQ_GOV_LIMITS);
766
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200767 pr_debug("CPU already managed, adding link\n");
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800768 ret = sysfs_create_link(&dev->kobj,
Dave Jonesecf7e462009-07-08 18:48:47 -0400769 &managed_policy->kobj,
770 "cpufreq");
771 if (ret)
772 cpufreq_cpu_put(managed_policy);
773 /*
774 * Success. We only needed to be added to the mask.
775 * Call driver->exit() because only the cpu parent of
776 * the kobj needed to call init().
777 */
778 if (cpufreq_driver->exit)
779 cpufreq_driver->exit(policy);
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200780
781 if (!ret)
782 return 1;
783 else
784 return ret;
Dave Jonesecf7e462009-07-08 18:48:47 -0400785 }
786 }
787#endif
788 return ret;
789}
790
791
Dave Jones19d6f7e2009-07-08 17:35:39 -0400792/* symlink affected CPUs */
Alex Chiangcf3289d02009-11-17 20:27:08 -0700793static int cpufreq_add_dev_symlink(unsigned int cpu,
794 struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400795{
796 unsigned int j;
797 int ret = 0;
798
799 for_each_cpu(j, policy->cpus) {
800 struct cpufreq_policy *managed_policy;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800801 struct device *cpu_dev;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400802
803 if (j == cpu)
804 continue;
805 if (!cpu_online(j))
806 continue;
807
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200808 pr_debug("CPU %u already managed, adding link\n", j);
Dave Jones19d6f7e2009-07-08 17:35:39 -0400809 managed_policy = cpufreq_cpu_get(cpu);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800810 cpu_dev = get_cpu_device(j);
811 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Dave Jones19d6f7e2009-07-08 17:35:39 -0400812 "cpufreq");
813 if (ret) {
814 cpufreq_cpu_put(managed_policy);
815 return ret;
816 }
817 }
818 return ret;
819}
820
Alex Chiangcf3289d02009-11-17 20:27:08 -0700821static int cpufreq_add_dev_interface(unsigned int cpu,
822 struct cpufreq_policy *policy,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800823 struct device *dev)
Dave Jones909a6942009-07-08 18:05:42 -0400824{
Dave Jonesecf7e462009-07-08 18:48:47 -0400825 struct cpufreq_policy new_policy;
Dave Jones909a6942009-07-08 18:05:42 -0400826 struct freq_attr **drv_attr;
827 unsigned long flags;
828 int ret = 0;
829 unsigned int j;
830
831 /* prepare interface data */
832 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800833 &dev->kobj, "cpufreq");
Dave Jones909a6942009-07-08 18:05:42 -0400834 if (ret)
835 return ret;
836
837 /* set up files for this cpu device */
838 drv_attr = cpufreq_driver->attr;
839 while ((drv_attr) && (*drv_attr)) {
840 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
841 if (ret)
842 goto err_out_kobj_put;
843 drv_attr++;
844 }
845 if (cpufreq_driver->get) {
846 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
847 if (ret)
848 goto err_out_kobj_put;
849 }
850 if (cpufreq_driver->target) {
851 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
852 if (ret)
853 goto err_out_kobj_put;
854 }
Thomas Renningere2f74f32009-11-19 12:31:01 +0100855 if (cpufreq_driver->bios_limit) {
856 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
857 if (ret)
858 goto err_out_kobj_put;
859 }
Dave Jones909a6942009-07-08 18:05:42 -0400860
861 spin_lock_irqsave(&cpufreq_driver_lock, flags);
862 for_each_cpu(j, policy->cpus) {
Julia Lawallbec037a2010-08-05 22:23:00 +0200863 if (!cpu_online(j))
864 continue;
Dave Jones909a6942009-07-08 18:05:42 -0400865 per_cpu(cpufreq_cpu_data, j) = policy;
Tejun Heof1625062009-10-29 22:34:13 +0900866 per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
Dave Jones909a6942009-07-08 18:05:42 -0400867 }
868 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
869
870 ret = cpufreq_add_dev_symlink(cpu, policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400871 if (ret)
872 goto err_out_kobj_put;
873
874 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
875 /* assure that the starting sequence is run in __cpufreq_set_policy */
876 policy->governor = NULL;
877
878 /* set default policy */
879 ret = __cpufreq_set_policy(policy, &new_policy);
880 policy->user_policy.policy = policy->policy;
881 policy->user_policy.governor = policy->governor;
882
883 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200884 pr_debug("setting policy failed\n");
Dave Jonesecf7e462009-07-08 18:48:47 -0400885 if (cpufreq_driver->exit)
886 cpufreq_driver->exit(policy);
887 }
Dave Jones909a6942009-07-08 18:05:42 -0400888 return ret;
889
890err_out_kobj_put:
891 kobject_put(&policy->kobj);
892 wait_for_completion(&policy->kobj_unregister);
893 return ret;
894}
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897/**
898 * cpufreq_add_dev - add a CPU device
899 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500900 * Adds the cpufreq interface for a CPU device.
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400901 *
902 * The Oracle says: try running cpufreq registration/unregistration concurrently
903 * with with cpu hotplugging and all hell will break loose. Tried to clean this
904 * mess up, but more thorough testing is needed. - Mathieu
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800906static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800908 unsigned int cpu = dev->id;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500909 int ret = 0, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 unsigned long flags;
912 unsigned int j;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500913#ifdef CONFIG_HOTPLUG_CPU
914 int sibling;
915#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Ashok Rajc32b6b82005-10-30 14:59:54 -0800917 if (cpu_is_offline(cpu))
918 return 0;
919
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200920 pr_debug("adding CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922#ifdef CONFIG_SMP
923 /* check whether a different CPU already registered this
924 * CPU because it is in the same boat. */
925 policy = cpufreq_cpu_get(cpu);
926 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -0500927 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return 0;
929 }
930#endif
931
932 if (!try_module_get(cpufreq_driver->owner)) {
933 ret = -EINVAL;
934 goto module_out;
935 }
936
Dave Jones059019a2009-07-08 16:30:03 -0400937 ret = -ENOMEM;
Dave Jonese98df502005-10-20 15:17:43 -0700938 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
Dave Jones059019a2009-07-08 16:30:03 -0400939 if (!policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 goto nomem_out;
Dave Jones059019a2009-07-08 16:30:03 -0400941
942 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400943 goto err_free_policy;
Dave Jones059019a2009-07-08 16:30:03 -0400944
945 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400946 goto err_free_cpumask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 policy->cpu = cpu;
Rusty Russell835481d2009-01-04 05:18:06 -0800949 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800951 /* Initially set CPU itself as the policy_cpu */
Tejun Heof1625062009-10-29 22:34:13 +0900952 per_cpu(cpufreq_policy_cpu, cpu) = cpu;
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400953 ret = (lock_policy_rwsem_write(cpu) < 0);
954 WARN_ON(ret);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 init_completion(&policy->kobj_unregister);
David Howells65f27f32006-11-22 14:55:48 +0000957 INIT_WORK(&policy->update, handle_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Thomas Renninger8122c6c2007-10-02 13:28:09 -0700959 /* Set governor before ->init, so that driver could check it */
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500960#ifdef CONFIG_HOTPLUG_CPU
961 for_each_online_cpu(sibling) {
962 struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
963 if (cp && cp->governor &&
964 (cpumask_test_cpu(cpu, cp->related_cpus))) {
965 policy->governor = cp->governor;
966 found = 1;
967 break;
968 }
969 }
970#endif
971 if (!found)
972 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 /* call driver. From then on the cpufreq must be able
974 * to accept all calls to ->verify and ->setpolicy for this CPU
975 */
976 ret = cpufreq_driver->init(policy);
977 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200978 pr_debug("initialization failed\n");
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400979 goto err_unlock_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 }
Viresh Kumar643ae6e2013-01-12 05:14:38 +0000981
982 /*
983 * affected cpus must always be the one, which are online. We aren't
984 * managing offline cpus here.
985 */
986 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
Viresh Kumar6954ca92013-01-12 05:14:40 +0000987 cpumask_and(policy->cpus, policy->cpus, &cpufreq_online_mask);
Viresh Kumar643ae6e2013-01-12 05:14:38 +0000988
Mike Chan187d9f42008-12-04 12:19:17 -0800989 policy->user_policy.min = policy->min;
990 policy->user_policy.max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Thomas Renningera1531ac2008-07-29 22:32:58 -0700992 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
993 CPUFREQ_START, policy);
994
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800995 ret = cpufreq_add_dev_policy(cpu, policy, dev);
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200996 if (ret) {
997 if (ret > 0)
998 /* This is a managed cpu, symlink created,
999 exit with 0 */
1000 ret = 0;
Dave Jonesecf7e462009-07-08 18:48:47 -04001001 goto err_unlock_policy;
Thomas Renninger4bfa0422009-07-24 15:25:03 +02001002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001004 ret = cpufreq_add_dev_interface(cpu, policy, dev);
Dave Jones19d6f7e2009-07-08 17:35:39 -04001005 if (ret)
1006 goto err_out_unregister;
Dave Jones8ff69732006-03-05 03:37:23 -05001007
Lothar Waßmanndca02612008-05-29 17:54:52 +02001008 unlock_policy_rwsem_write(cpu);
1009
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001010 kobject_uevent(&policy->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 module_put(cpufreq_driver->owner);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001012 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 return 0;
1015
1016
1017err_out_unregister:
1018 spin_lock_irqsave(&cpufreq_driver_lock, flags);
Rusty Russell835481d2009-01-04 05:18:06 -08001019 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -07001020 per_cpu(cpufreq_cpu_data, j) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1022
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -08001023 kobject_put(&policy->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 wait_for_completion(&policy->kobj_unregister);
1025
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001026err_unlock_policy:
Dave Jones45709112008-03-05 14:07:34 -05001027 unlock_policy_rwsem_write(cpu);
Xiaotian Fengcad70a62010-07-20 20:11:02 +08001028 free_cpumask_var(policy->related_cpus);
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001029err_free_cpumask:
1030 free_cpumask_var(policy->cpus);
1031err_free_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 kfree(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033nomem_out:
1034 module_put(cpufreq_driver->owner);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001035module_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 return ret;
1037}
1038
1039
1040/**
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001041 * __cpufreq_remove_dev - remove a CPU device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 *
1043 * Removes the cpufreq interface for a CPU device.
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001044 * Caller should already have policy_rwsem in write mode for this CPU.
1045 * This routine frees the rwsem before returning.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001047static int __cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001049 unsigned int cpu = dev->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 unsigned long flags;
1051 struct cpufreq_policy *data;
Amerigo Wang499bca92010-03-04 03:23:46 -05001052 struct kobject *kobj;
1053 struct completion *cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054#ifdef CONFIG_SMP
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001055 struct device *cpu_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 unsigned int j;
1057#endif
1058
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001059 pr_debug("unregistering CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 spin_lock_irqsave(&cpufreq_driver_lock, flags);
Mike Travis7a6aedf2008-03-25 15:06:53 -07001062 data = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 if (!data) {
1065 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001066 unlock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 return -EINVAL;
1068 }
Mike Travis7a6aedf2008-03-25 15:06:53 -07001069 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071#ifdef CONFIG_SMP
1072 /* if this isn't the CPU which is the parent of the kobj, we
Dave Jones32ee8c32006-02-28 00:43:23 -05001073 * only need to unlink, put and exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 */
1075 if (unlikely(cpu != data->cpu)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001076 pr_debug("removing link\n");
Viresh Kumarf6a74092013-01-12 05:14:39 +00001077 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Rusty Russell835481d2009-01-04 05:18:06 -08001078 cpumask_clear_cpu(cpu, data->cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarf6a74092013-01-12 05:14:39 +00001080
1081 __cpufreq_governor(data, CPUFREQ_GOV_START);
1082 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1083
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001084 kobj = &dev->kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 cpufreq_cpu_put(data);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001086 unlock_policy_rwsem_write(cpu);
Amerigo Wang499bca92010-03-04 03:23:46 -05001087 sysfs_remove_link(kobj, "cpufreq");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return 0;
1089 }
1090#endif
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092#ifdef CONFIG_SMP
Thomas Renninger084f3492007-07-09 11:35:28 -07001093
1094#ifdef CONFIG_HOTPLUG_CPU
Dmitry Monakhove77b89f2009-10-05 00:38:55 +04001095 strncpy(per_cpu(cpufreq_cpu_governor, cpu), data->governor->name,
1096 CPUFREQ_NAME_LEN);
Thomas Renninger084f3492007-07-09 11:35:28 -07001097#endif
1098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 /* if we have other CPUs still registered, we need to unlink them,
1100 * or else wait_for_completion below will lock up. Clean the
Mike Travis7a6aedf2008-03-25 15:06:53 -07001101 * per_cpu(cpufreq_cpu_data) while holding the lock, and remove
1102 * the sysfs links afterwards.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 */
Rusty Russell835481d2009-01-04 05:18:06 -08001104 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1105 for_each_cpu(j, data->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (j == cpu)
1107 continue;
Mike Travis7a6aedf2008-03-25 15:06:53 -07001108 per_cpu(cpufreq_cpu_data, j) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
1110 }
1111
1112 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1113
Rusty Russell835481d2009-01-04 05:18:06 -08001114 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1115 for_each_cpu(j, data->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 if (j == cpu)
1117 continue;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001118 pr_debug("removing link for cpu %u\n", j);
Thomas Renninger084f3492007-07-09 11:35:28 -07001119#ifdef CONFIG_HOTPLUG_CPU
Dmitry Monakhove77b89f2009-10-05 00:38:55 +04001120 strncpy(per_cpu(cpufreq_cpu_governor, j),
1121 data->governor->name, CPUFREQ_NAME_LEN);
Thomas Renninger084f3492007-07-09 11:35:28 -07001122#endif
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001123 cpu_dev = get_cpu_device(j);
1124 kobj = &cpu_dev->kobj;
Amerigo Wang499bca92010-03-04 03:23:46 -05001125 unlock_policy_rwsem_write(cpu);
1126 sysfs_remove_link(kobj, "cpufreq");
1127 lock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 cpufreq_cpu_put(data);
1129 }
1130 }
1131#else
1132 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1133#endif
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (cpufreq_driver->target)
1136 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001137
Amerigo Wang499bca92010-03-04 03:23:46 -05001138 kobj = &data->kobj;
1139 cmp = &data->kobj_unregister;
1140 unlock_policy_rwsem_write(cpu);
1141 kobject_put(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143 /* we need to make sure that the underlying kobj is actually
Dave Jones32ee8c32006-02-28 00:43:23 -05001144 * not referenced anymore by anybody before we proceed with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 * unloading.
1146 */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001147 pr_debug("waiting for dropping of refcount\n");
Amerigo Wang499bca92010-03-04 03:23:46 -05001148 wait_for_completion(cmp);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001149 pr_debug("wait complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Amerigo Wang499bca92010-03-04 03:23:46 -05001151 lock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if (cpufreq_driver->exit)
1153 cpufreq_driver->exit(data);
venkatesh.pallipadi@intel.com7d26e2d2009-07-02 17:08:30 -07001154 unlock_policy_rwsem_write(cpu);
1155
Jacob Shin27ecddc2011-04-27 13:32:11 -05001156#ifdef CONFIG_HOTPLUG_CPU
1157 /* when the CPU which is the parent of the kobj is hotplugged
1158 * offline, check for siblings, and create cpufreq sysfs interface
1159 * and symlinks
1160 */
1161 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1162 /* first sibling now owns the new sysfs dir */
1163 cpumask_clear_cpu(cpu, data->cpus);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001164 cpufreq_add_dev(get_cpu_device(cpumask_first(data->cpus)), NULL);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001165
1166 /* finally remove our own symlink */
1167 lock_policy_rwsem_write(cpu);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001168 __cpufreq_remove_dev(dev, sif);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001169 }
1170#endif
1171
Rusty Russell835481d2009-01-04 05:18:06 -08001172 free_cpumask_var(data->related_cpus);
1173 free_cpumask_var(data->cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 kfree(data);
1175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 return 0;
1177}
1178
1179
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001180static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001181{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001182 unsigned int cpu = dev->id;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001183 int retval;
Venki Pallipadiec282972007-03-26 12:03:19 -07001184
1185 if (cpu_is_offline(cpu))
1186 return 0;
1187
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001188 if (unlikely(lock_policy_rwsem_write(cpu)))
1189 BUG();
1190
Viresh Kumar6954ca92013-01-12 05:14:40 +00001191 cpumask_clear_cpu(cpu, &cpufreq_online_mask);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001192 retval = __cpufreq_remove_dev(dev, sif);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001193 return retval;
1194}
1195
1196
David Howells65f27f32006-11-22 14:55:48 +00001197static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
David Howells65f27f32006-11-22 14:55:48 +00001199 struct cpufreq_policy *policy =
1200 container_of(work, struct cpufreq_policy, update);
1201 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001202 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 cpufreq_update_policy(cpu);
1204}
1205
1206/**
1207 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
1208 * @cpu: cpu number
1209 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1210 * @new_freq: CPU frequency the CPU actually runs at
1211 *
Dave Jones29464f22009-01-18 01:37:11 -05001212 * We adjust to current frequency first, and need to clean up later.
1213 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301215static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1216 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
1218 struct cpufreq_freqs freqs;
1219
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001220 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1222
1223 freqs.cpu = cpu;
1224 freqs.old = old_freq;
1225 freqs.new = new_freq;
1226 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
1227 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
1228}
1229
1230
Dave Jones32ee8c32006-02-28 00:43:23 -05001231/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301232 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001233 * @cpu: CPU number
1234 *
1235 * This is the last known freq, without actually getting it from the driver.
1236 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1237 */
1238unsigned int cpufreq_quick_get(unsigned int cpu)
1239{
1240 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301241 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001242
1243 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301244 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001245 cpufreq_cpu_put(policy);
1246 }
1247
Dave Jones4d34a672008-02-07 16:33:49 -05001248 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001249}
1250EXPORT_SYMBOL(cpufreq_quick_get);
1251
Jesse Barnes3d737102011-06-28 10:59:12 -07001252/**
1253 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1254 * @cpu: CPU number
1255 *
1256 * Just return the max possible frequency for a given CPU.
1257 */
1258unsigned int cpufreq_quick_get_max(unsigned int cpu)
1259{
1260 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1261 unsigned int ret_freq = 0;
1262
1263 if (policy) {
1264 ret_freq = policy->max;
1265 cpufreq_cpu_put(policy);
1266 }
1267
1268 return ret_freq;
1269}
1270EXPORT_SYMBOL(cpufreq_quick_get_max);
1271
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001272
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001273static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001275 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301276 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001279 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301281 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301283 if (ret_freq && policy->cur &&
1284 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1285 /* verify no discrepancy between actual and
1286 saved value exists */
1287 if (unlikely(ret_freq != policy->cur)) {
1288 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 schedule_work(&policy->update);
1290 }
1291 }
1292
Dave Jones4d34a672008-02-07 16:33:49 -05001293 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001294}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001296/**
1297 * cpufreq_get - get the current CPU frequency (in kHz)
1298 * @cpu: CPU number
1299 *
1300 * Get the CPU current (static) CPU frequency
1301 */
1302unsigned int cpufreq_get(unsigned int cpu)
1303{
1304 unsigned int ret_freq = 0;
1305 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1306
1307 if (!policy)
1308 goto out;
1309
1310 if (unlikely(lock_policy_rwsem_read(cpu)))
1311 goto out_policy;
1312
1313 ret_freq = __cpufreq_get(cpu);
1314
1315 unlock_policy_rwsem_read(cpu);
1316
1317out_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 cpufreq_cpu_put(policy);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001319out:
Dave Jones4d34a672008-02-07 16:33:49 -05001320 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321}
1322EXPORT_SYMBOL(cpufreq_get);
1323
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001324static struct subsys_interface cpufreq_interface = {
1325 .name = "cpufreq",
1326 .subsys = &cpu_subsys,
1327 .add_dev = cpufreq_add_dev,
1328 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001329};
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001333 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1334 *
1335 * This function is only executed for the boot processor. The other CPUs
1336 * have been put offline by means of CPU hotplug.
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001337 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001338static int cpufreq_bp_suspend(void)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001339{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301340 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001341
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001342 int cpu = smp_processor_id();
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001343 struct cpufreq_policy *cpu_policy;
1344
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001345 pr_debug("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001346
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001347 /* If there's no policy for the boot CPU, we have nothing to do. */
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001348 cpu_policy = cpufreq_cpu_get(cpu);
1349 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001350 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001351
1352 if (cpufreq_driver->suspend) {
Rafael J. Wysocki7ca64e22011-03-10 21:13:05 +01001353 ret = cpufreq_driver->suspend(cpu_policy);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001354 if (ret)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001355 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1356 "step on CPU %u\n", cpu_policy->cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001357 }
1358
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001359 cpufreq_cpu_put(cpu_policy);
Dave Jonesc9060492008-02-07 16:32:18 -05001360 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001361}
1362
1363/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001364 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 *
1366 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001367 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1368 * restored. It will verify that the current freq is in sync with
1369 * what we believe it to be. This is a bit later than when it
1370 * should be, but nonethteless it's better than calling
1371 * cpufreq_driver->get() here which might re-enable interrupts...
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001372 *
1373 * This function is only executed for the boot CPU. The other CPUs have not
1374 * been turned on yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001376static void cpufreq_bp_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301378 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001379
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001380 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 struct cpufreq_policy *cpu_policy;
1382
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001383 pr_debug("resuming cpu %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001385 /* If there's no policy for the boot CPU, we have nothing to do. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 cpu_policy = cpufreq_cpu_get(cpu);
1387 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001388 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390 if (cpufreq_driver->resume) {
1391 ret = cpufreq_driver->resume(cpu_policy);
1392 if (ret) {
1393 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1394 "step on CPU %u\n", cpu_policy->cpu);
Dave Jonesc9060492008-02-07 16:32:18 -05001395 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
1397 }
1398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 schedule_work(&cpu_policy->update);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001400
Dave Jonesc9060492008-02-07 16:32:18 -05001401fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403}
1404
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001405static struct syscore_ops cpufreq_syscore_ops = {
1406 .suspend = cpufreq_bp_suspend,
1407 .resume = cpufreq_bp_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408};
1409
1410
1411/*********************************************************************
1412 * NOTIFIER LISTS INTERFACE *
1413 *********************************************************************/
1414
1415/**
1416 * cpufreq_register_notifier - register a driver with cpufreq
1417 * @nb: notifier function to register
1418 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1419 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001420 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 * are notified about clock rate changes (once before and once after
1422 * the transition), or a list of drivers that are notified about
1423 * changes in cpufreq policy.
1424 *
1425 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001426 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 */
1428int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1429{
1430 int ret;
1431
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001432 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 switch (list) {
1435 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001436 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001437 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 break;
1439 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001440 ret = blocking_notifier_chain_register(
1441 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 break;
1443 default:
1444 ret = -EINVAL;
1445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447 return ret;
1448}
1449EXPORT_SYMBOL(cpufreq_register_notifier);
1450
1451
1452/**
1453 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1454 * @nb: notifier block to be unregistered
1455 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1456 *
1457 * Remove a driver from the CPU frequency notifier list.
1458 *
1459 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001460 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 */
1462int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1463{
1464 int ret;
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 switch (list) {
1467 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001468 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001469 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 break;
1471 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001472 ret = blocking_notifier_chain_unregister(
1473 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 break;
1475 default:
1476 ret = -EINVAL;
1477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
1479 return ret;
1480}
1481EXPORT_SYMBOL(cpufreq_unregister_notifier);
1482
1483
1484/*********************************************************************
1485 * GOVERNORS *
1486 *********************************************************************/
1487
1488
1489int __cpufreq_driver_target(struct cpufreq_policy *policy,
1490 unsigned int target_freq,
1491 unsigned int relation)
1492{
1493 int retval = -EINVAL;
Viresh Kumar72499242012-10-31 01:28:21 +01001494 unsigned int old_target_freq = target_freq;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001495
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001496 if (cpufreq_disabled())
1497 return -ENODEV;
1498
Viresh Kumar72499242012-10-31 01:28:21 +01001499 /* Make sure that target_freq is within supported range */
1500 if (target_freq > policy->max)
1501 target_freq = policy->max;
1502 if (target_freq < policy->min)
1503 target_freq = policy->min;
1504
1505 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1506 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001507
1508 if (target_freq == policy->cur)
1509 return 0;
1510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1512 retval = cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 return retval;
1515}
1516EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518int cpufreq_driver_target(struct cpufreq_policy *policy,
1519 unsigned int target_freq,
1520 unsigned int relation)
1521{
Julia Lawallf1829e42008-07-25 22:44:53 +02001522 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524 policy = cpufreq_cpu_get(policy->cpu);
1525 if (!policy)
Julia Lawallf1829e42008-07-25 22:44:53 +02001526 goto no_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001528 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
Julia Lawallf1829e42008-07-25 22:44:53 +02001529 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531 ret = __cpufreq_driver_target(policy, target_freq, relation);
1532
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001533 unlock_policy_rwsem_write(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Julia Lawallf1829e42008-07-25 22:44:53 +02001535fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 cpufreq_cpu_put(policy);
Julia Lawallf1829e42008-07-25 22:44:53 +02001537no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 return ret;
1539}
1540EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1541
venkatesh.pallipadi@intel.combf0b90e2008-08-04 11:59:07 -07001542int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu)
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001543{
1544 int ret = 0;
1545
Viresh Kumar0676f7f2012-10-24 23:39:48 +02001546 if (!(cpu_online(cpu) && cpufreq_driver->getavg))
1547 return 0;
1548
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001549 policy = cpufreq_cpu_get(policy->cpu);
1550 if (!policy)
1551 return -EINVAL;
1552
Viresh Kumar0676f7f2012-10-24 23:39:48 +02001553 ret = cpufreq_driver->getavg(policy, cpu);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001554
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001555 cpufreq_cpu_put(policy);
1556 return ret;
1557}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001558EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001559
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001560/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001561 * when "event" is CPUFREQ_GOV_LIMITS
1562 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301564static int __cpufreq_governor(struct cpufreq_policy *policy,
1565 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
Dave Jonescc993ca2005-07-28 09:43:56 -07001567 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001568
1569 /* Only must be defined when default governor is known to have latency
1570 restrictions, like e.g. conservative or ondemand.
1571 That this is the case is already ensured in Kconfig
1572 */
1573#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1574 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1575#else
1576 struct cpufreq_governor *gov = NULL;
1577#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001578
1579 if (policy->governor->max_transition_latency &&
1580 policy->cpuinfo.transition_latency >
1581 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07001582 if (!gov)
1583 return -EINVAL;
1584 else {
1585 printk(KERN_WARNING "%s governor failed, too long"
1586 " transition latency of HW, fallback"
1587 " to %s governor\n",
1588 policy->governor->name,
1589 gov->name);
1590 policy->governor = gov;
1591 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
1594 if (!try_module_get(policy->governor->owner))
1595 return -EINVAL;
1596
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001597 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301598 policy->cpu, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 ret = policy->governor->governor(policy, event);
1600
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301601 /* we keep one module reference alive for
1602 each CPU governed by this CPU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 if ((event != CPUFREQ_GOV_START) || ret)
1604 module_put(policy->governor->owner);
1605 if ((event == CPUFREQ_GOV_STOP) && !ret)
1606 module_put(policy->governor->owner);
1607
1608 return ret;
1609}
1610
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612int cpufreq_register_governor(struct cpufreq_governor *governor)
1613{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001614 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616 if (!governor)
1617 return -EINVAL;
1618
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001619 if (cpufreq_disabled())
1620 return -ENODEV;
1621
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001622 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001623
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001624 err = -EBUSY;
1625 if (__find_governor(governor->name) == NULL) {
1626 err = 0;
1627 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Dave Jones32ee8c32006-02-28 00:43:23 -05001630 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001631 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632}
1633EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1634
1635
1636void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1637{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001638#ifdef CONFIG_HOTPLUG_CPU
1639 int cpu;
1640#endif
1641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 if (!governor)
1643 return;
1644
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001645 if (cpufreq_disabled())
1646 return;
1647
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001648#ifdef CONFIG_HOTPLUG_CPU
1649 for_each_present_cpu(cpu) {
1650 if (cpu_online(cpu))
1651 continue;
1652 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1653 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1654 }
1655#endif
1656
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001657 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001659 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 return;
1661}
1662EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1663
1664
1665
1666/*********************************************************************
1667 * POLICY INTERFACE *
1668 *********************************************************************/
1669
1670/**
1671 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05001672 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1673 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 *
1675 * Reads the current cpufreq policy.
1676 */
1677int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1678{
1679 struct cpufreq_policy *cpu_policy;
1680 if (!policy)
1681 return -EINVAL;
1682
1683 cpu_policy = cpufreq_cpu_get(cpu);
1684 if (!cpu_policy)
1685 return -EINVAL;
1686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 return 0;
1691}
1692EXPORT_SYMBOL(cpufreq_get_policy);
1693
1694
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001695/*
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301696 * data : current policy.
1697 * policy : policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001698 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301699static int __cpufreq_set_policy(struct cpufreq_policy *data,
1700 struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
1702 int ret = 0;
1703
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001704 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 policy->min, policy->max);
1706
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301707 memcpy(&policy->cpuinfo, &data->cpuinfo,
1708 sizeof(struct cpufreq_cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Yi Yang53391fa2008-01-30 13:33:34 +01001710 if (policy->min > data->max || policy->max < data->min) {
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02001711 ret = -EINVAL;
1712 goto error_out;
1713 }
1714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 /* verify the cpu speed can be set within this limit */
1716 ret = cpufreq_driver->verify(policy);
1717 if (ret)
1718 goto error_out;
1719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001721 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1722 CPUFREQ_ADJUST, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001725 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1726 CPUFREQ_INCOMPATIBLE, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
1728 /* verify the cpu speed can be set within this limit,
1729 which might be different to the first one */
1730 ret = cpufreq_driver->verify(policy);
Alan Sterne041c682006-03-27 01:16:30 -08001731 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
1734 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001735 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1736 CPUFREQ_NOTIFY, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Dave Jones7d5e3502006-02-02 17:03:42 -05001738 data->min = policy->min;
1739 data->max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001741 pr_debug("new min and max freqs are %u - %u kHz\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301742 data->min, data->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
1744 if (cpufreq_driver->setpolicy) {
1745 data->policy = policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001746 pr_debug("setting range\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 ret = cpufreq_driver->setpolicy(policy);
1748 } else {
1749 if (policy->governor != data->governor) {
1750 /* save old, working values */
1751 struct cpufreq_governor *old_gov = data->governor;
1752
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001753 pr_debug("governor switch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
1755 /* end old governor */
Andrej Gelenbergffe62752010-05-14 15:15:58 -07001756 if (data->governor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1758
1759 /* start new governor */
1760 data->governor = policy->governor;
1761 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1762 /* new governor failed, so re-start old one */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001763 pr_debug("starting governor %s failed\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301764 data->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 if (old_gov) {
1766 data->governor = old_gov;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301767 __cpufreq_governor(data,
1768 CPUFREQ_GOV_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 }
1770 ret = -EINVAL;
1771 goto error_out;
1772 }
1773 /* might be a policy change, too, so fall through */
1774 }
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001775 pr_debug("governor: change or update limits\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1777 }
1778
Dave Jones7d5e3502006-02-02 17:03:42 -05001779error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 return ret;
1781}
1782
1783/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1785 * @cpu: CPU which shall be re-evaluated
1786 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001787 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 * at different times.
1789 */
1790int cpufreq_update_policy(unsigned int cpu)
1791{
1792 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1793 struct cpufreq_policy policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02001794 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Julia Lawallf1829e42008-07-25 22:44:53 +02001796 if (!data) {
1797 ret = -ENODEV;
1798 goto no_policy;
1799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Julia Lawallf1829e42008-07-25 22:44:53 +02001801 if (unlikely(lock_policy_rwsem_write(cpu))) {
1802 ret = -EINVAL;
1803 goto fail;
1804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001806 pr_debug("updating policy for CPU %u\n", cpu);
Dave Jones7d5e3502006-02-02 17:03:42 -05001807 memcpy(&policy, data, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 policy.min = data->user_policy.min;
1809 policy.max = data->user_policy.max;
1810 policy.policy = data->user_policy.policy;
1811 policy.governor = data->user_policy.governor;
1812
Thomas Renninger0961dd02006-01-26 18:46:33 +01001813 /* BIOS might change freq behind our back
1814 -> ask driver for current freq and notify governors about a change */
1815 if (cpufreq_driver->get) {
1816 policy.cur = cpufreq_driver->get(cpu);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001817 if (!data->cur) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001818 pr_debug("Driver did not initialize current freq");
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001819 data->cur = policy.cur;
1820 } else {
1821 if (data->cur != policy.cur)
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301822 cpufreq_out_of_sync(cpu, data->cur,
1823 policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001824 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01001825 }
1826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 ret = __cpufreq_set_policy(data, &policy);
1828
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001829 unlock_policy_rwsem_write(cpu);
1830
Julia Lawallf1829e42008-07-25 22:44:53 +02001831fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 cpufreq_cpu_put(data);
Julia Lawallf1829e42008-07-25 22:44:53 +02001833no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 return ret;
1835}
1836EXPORT_SYMBOL(cpufreq_update_policy);
1837
Satyam Sharmadd184a02007-10-02 13:28:14 -07001838static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08001839 unsigned long action, void *hcpu)
1840{
1841 unsigned int cpu = (unsigned long)hcpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001842 struct device *dev;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001843
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001844 dev = get_cpu_device(cpu);
1845 if (dev) {
Ashok Rajc32b6b82005-10-30 14:59:54 -08001846 switch (action) {
1847 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001848 case CPU_ONLINE_FROZEN:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001849 cpufreq_add_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001850 break;
1851 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001852 case CPU_DOWN_PREPARE_FROZEN:
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001853 if (unlikely(lock_policy_rwsem_write(cpu)))
1854 BUG();
1855
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001856 __cpufreq_remove_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001857 break;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001858 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001859 case CPU_DOWN_FAILED_FROZEN:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001860 cpufreq_add_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001861 break;
1862 }
1863 }
1864 return NOTIFY_OK;
1865}
1866
Neal Buckendahl9c36f742010-06-22 22:02:44 -05001867static struct notifier_block __refdata cpufreq_cpu_notifier = {
Ashok Rajc32b6b82005-10-30 14:59:54 -08001868 .notifier_call = cpufreq_cpu_callback,
1869};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
1871/*********************************************************************
1872 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1873 *********************************************************************/
1874
1875/**
1876 * cpufreq_register_driver - register a CPU Frequency driver
1877 * @driver_data: A struct cpufreq_driver containing the values#
1878 * submitted by the CPU Frequency driver.
1879 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001880 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05001882 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 *
1884 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001885int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
1887 unsigned long flags;
1888 int ret;
1889
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001890 if (cpufreq_disabled())
1891 return -ENODEV;
1892
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 if (!driver_data || !driver_data->verify || !driver_data->init ||
1894 ((!driver_data->setpolicy) && (!driver_data->target)))
1895 return -EINVAL;
1896
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001897 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
1899 if (driver_data->setpolicy)
1900 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1901
1902 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1903 if (cpufreq_driver) {
1904 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1905 return -EBUSY;
1906 }
1907 cpufreq_driver = driver_data;
1908 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1909
Viresh Kumar6954ca92013-01-12 05:14:40 +00001910 cpumask_setall(&cpufreq_online_mask);
1911
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001912 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001913 if (ret)
1914 goto err_null_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001916 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 int i;
1918 ret = -ENODEV;
1919
1920 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07001921 for (i = 0; i < nr_cpu_ids; i++)
1922 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07001924 break;
1925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
1927 /* if all ->init() calls failed, unregister */
1928 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001929 pr_debug("no CPU initialized for driver %s\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301930 driver_data->name);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001931 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 }
1933 }
1934
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001935 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001936 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001938 return 0;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001939err_if_unreg:
1940 subsys_interface_unregister(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001941err_null_driver:
1942 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1943 cpufreq_driver = NULL;
1944 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05001945 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946}
1947EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1948
1949
1950/**
1951 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1952 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001953 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 * the right to do so, i.e. if you have succeeded in initialising before!
1955 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1956 * currently not initialised.
1957 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001958int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
1960 unsigned long flags;
1961
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001962 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001965 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001967 subsys_interface_unregister(&cpufreq_interface);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001968 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1971 cpufreq_driver = NULL;
1972 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1973
1974 return 0;
1975}
1976EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001977
1978static int __init cpufreq_core_init(void)
1979{
1980 int cpu;
1981
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001982 if (cpufreq_disabled())
1983 return -ENODEV;
1984
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001985 for_each_possible_cpu(cpu) {
Tejun Heof1625062009-10-29 22:34:13 +09001986 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001987 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
1988 }
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02001989
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001990 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02001991 BUG_ON(!cpufreq_global_kobject);
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001992 register_syscore_ops(&cpufreq_syscore_ops);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02001993
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001994 return 0;
1995}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001996core_initcall(cpufreq_core_init);