blob: 3d5614b13b3cc353e4df3608544c6b057b1dedad [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/cpufreq/cpufreq.c
3 *
4 * Copyright (C) 2001 Russell King
5 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
6 *
Ashok Rajc32b6b82005-10-30 14:59:54 -08007 * Oct 2005 - Ashok Raj <ashok.raj@intel.com>
Dave Jones32ee8c32006-02-28 00:43:23 -05008 * Added handling for CPU hotplug
Dave Jones8ff69732006-03-05 03:37:23 -05009 * Feb 2006 - Jacob Shin <jacob.shin@amd.com>
10 * Fix handling for CPU hotplug -- affected CPUs
Ashok Rajc32b6b82005-10-30 14:59:54 -080011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 *
16 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/init.h>
21#include <linux/notifier.h>
22#include <linux/cpufreq.h>
23#include <linux/delay.h>
24#include <linux/interrupt.h>
25#include <linux/spinlock.h>
26#include <linux/device.h>
27#include <linux/slab.h>
28#include <linux/cpu.h>
29#include <linux/completion.h>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080030#include <linux/mutex.h>
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +010031#include <linux/syscore_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Thomas Renninger6f4f2722010-04-20 13:17:36 +020033#include <trace/events/power.h>
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035/**
Dave Jonescd878472006-08-11 17:59:28 -040036 * The "cpufreq driver" - the arch- or hardware-dependent low
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * level driver of CPUFreq support, and its spinlock. This lock
38 * also protects the cpufreq_cpu_data array.
39 */
Dave Jones7d5e3502006-02-02 17:03:42 -050040static struct cpufreq_driver *cpufreq_driver;
Mike Travis7a6aedf2008-03-25 15:06:53 -070041static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
Thomas Renninger084f3492007-07-09 11:35:28 -070042#ifdef CONFIG_HOTPLUG_CPU
43/* This one keeps track of the previously set governor of a removed CPU */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070044struct cpufreq_cpu_save_data {
45 char gov[CPUFREQ_NAME_LEN];
46 unsigned int max, min;
47};
48static DEFINE_PER_CPU(struct cpufreq_cpu_save_data, cpufreq_policy_save);
Thomas Renninger084f3492007-07-09 11:35:28 -070049#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static DEFINE_SPINLOCK(cpufreq_driver_lock);
51
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080052/*
53 * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
54 * all cpufreq/hotplug/workqueue/etc related lock issues.
55 *
56 * The rules for this semaphore:
57 * - Any routine that wants to read from the policy structure will
58 * do a down_read on this semaphore.
59 * - Any routine that will write to the policy structure and/or may take away
60 * the policy altogether (eg. CPU hotplug), will hold this lock in write
61 * mode before doing so.
62 *
63 * Additional rules:
64 * - All holders of the lock should check to make sure that the CPU they
65 * are concerned with are online after they get the lock.
66 * - Governor routines that can be called in cpufreq hotplug path should not
67 * take this sem as top level hotplug notifier handler takes this.
Mathieu Desnoyers395913d2009-06-08 13:17:31 -040068 * - Lock should not be held across
69 * __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080070 */
Tejun Heof1625062009-10-29 22:34:13 +090071static DEFINE_PER_CPU(int, cpufreq_policy_cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080072static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
73
74#define lock_policy_rwsem(mode, cpu) \
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070075int lock_policy_rwsem_##mode \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080076(int cpu) \
77{ \
Tejun Heof1625062009-10-29 22:34:13 +090078 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080079 BUG_ON(policy_cpu == -1); \
80 down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
81 if (unlikely(!cpu_online(cpu))) { \
82 up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
83 return -1; \
84 } \
85 \
86 return 0; \
87}
88
89lock_policy_rwsem(read, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080090
91lock_policy_rwsem(write, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080092
Amerigo Wang226528c2010-03-04 03:23:36 -050093static void unlock_policy_rwsem_read(int cpu)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080094{
Tejun Heof1625062009-10-29 22:34:13 +090095 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080096 BUG_ON(policy_cpu == -1);
97 up_read(&per_cpu(cpu_policy_rwsem, policy_cpu));
98}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080099
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700100void unlock_policy_rwsem_write(int cpu)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800101{
Tejun Heof1625062009-10-29 22:34:13 +0900102 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800103 BUG_ON(policy_cpu == -1);
104 up_write(&per_cpu(cpu_policy_rwsem, policy_cpu));
105}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800106
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/* internal prototypes */
Dave Jones29464f22009-01-18 01:37:11 -0500109static int __cpufreq_governor(struct cpufreq_policy *policy,
110 unsigned int event);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800111static unsigned int __cpufreq_get(unsigned int cpu);
David Howells65f27f32006-11-22 14:55:48 +0000112static void handle_update(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114/**
Dave Jones32ee8c32006-02-28 00:43:23 -0500115 * Two notifier lists: the "policy" list is involved in the
116 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 * "transition" list for kernel code that needs to handle
118 * changes to devices when the CPU clock speed changes.
119 * The mutex locks both lists.
120 */
Alan Sterne041c682006-03-27 01:16:30 -0800121static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700122static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200124static bool init_cpufreq_transition_notifier_list_called;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700125static int __init init_cpufreq_transition_notifier_list(void)
126{
127 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200128 init_cpufreq_transition_notifier_list_called = true;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700129 return 0;
130}
Linus Torvaldsb3438f82006-11-20 11:47:18 -0800131pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400133static int off __read_mostly;
134int cpufreq_disabled(void)
135{
136 return off;
137}
138void disable_cpufreq(void)
139{
140 off = 1;
141}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142static LIST_HEAD(cpufreq_governor_list);
Dave Jones29464f22009-01-18 01:37:11 -0500143static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Stephen Boydf82ef512012-07-17 14:33:57 -0700145static struct cpufreq_policy *__cpufreq_cpu_get(unsigned int cpu, int sysfs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 struct cpufreq_policy *data;
148 unsigned long flags;
149
Mike Travis7a6aedf2008-03-25 15:06:53 -0700150 if (cpu >= nr_cpu_ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 goto err_out;
152
153 /* get the cpufreq driver */
154 spin_lock_irqsave(&cpufreq_driver_lock, flags);
155
156 if (!cpufreq_driver)
157 goto err_out_unlock;
158
159 if (!try_module_get(cpufreq_driver->owner))
160 goto err_out_unlock;
161
162
163 /* get the CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -0700164 data = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 if (!data)
167 goto err_out_put_module;
168
Stephen Boydf82ef512012-07-17 14:33:57 -0700169 if (!sysfs && !kobject_get(&data->kobj))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 goto err_out_put_module;
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return data;
174
Dave Jones7d5e3502006-02-02 17:03:42 -0500175err_out_put_module:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 module_put(cpufreq_driver->owner);
Dave Jones7d5e3502006-02-02 17:03:42 -0500177err_out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones7d5e3502006-02-02 17:03:42 -0500179err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return NULL;
181}
Stephen Boydf82ef512012-07-17 14:33:57 -0700182
183struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
184{
185 return __cpufreq_cpu_get(cpu, 0);
186}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
188
Stephen Boydf82ef512012-07-17 14:33:57 -0700189static struct cpufreq_policy *cpufreq_cpu_get_sysfs(unsigned int cpu)
190{
191 return __cpufreq_cpu_get(cpu, 1);
192}
193
194static void __cpufreq_cpu_put(struct cpufreq_policy *data, int sysfs)
195{
196 if (!sysfs)
197 kobject_put(&data->kobj);
198 module_put(cpufreq_driver->owner);
199}
Dave Jones7d5e3502006-02-02 17:03:42 -0500200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201void cpufreq_cpu_put(struct cpufreq_policy *data)
202{
Stephen Boydf82ef512012-07-17 14:33:57 -0700203 __cpufreq_cpu_put(data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
206
Stephen Boydf82ef512012-07-17 14:33:57 -0700207static void cpufreq_cpu_put_sysfs(struct cpufreq_policy *data)
208{
209 __cpufreq_cpu_put(data, 1);
210}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
214 *********************************************************************/
215
216/**
217 * adjust_jiffies - adjust the system "loops_per_jiffy"
218 *
219 * This function alters the system "loops_per_jiffy" for the clock
220 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500221 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 * per-CPU loops_per_jiffy value wherever possible.
223 */
224#ifndef CONFIG_SMP
225static unsigned long l_p_j_ref;
226static unsigned int l_p_j_ref_freq;
227
Arjan van de Ven858119e2006-01-14 13:20:43 -0800228static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 if (ci->flags & CPUFREQ_CONST_LOOPS)
231 return;
232
233 if (!l_p_j_ref_freq) {
234 l_p_j_ref = loops_per_jiffy;
235 l_p_j_ref_freq = ci->old;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200236 pr_debug("saving %lu as reference value for loops_per_jiffy; "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530237 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
Afzal Mohammedd08de0c12012-01-04 10:52:46 +0530239 if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700240 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530241 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
242 ci->new);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200243 pr_debug("scaling loops_per_jiffy to %lu "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530244 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246}
247#else
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530248static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
249{
250 return;
251}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252#endif
253
254
255/**
Dave Jonese4472cb2006-01-31 15:53:55 -0800256 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
257 * on frequency transition.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 *
Dave Jonese4472cb2006-01-31 15:53:55 -0800259 * This function calls the transition notifiers and the "adjust_jiffies"
260 * function. It is called twice on all CPU frequency changes that have
Dave Jones32ee8c32006-02-28 00:43:23 -0500261 * external effects.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 */
263void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
264{
Dave Jonese4472cb2006-01-31 15:53:55 -0800265 struct cpufreq_policy *policy;
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 BUG_ON(irqs_disabled());
268
269 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200270 pr_debug("notification %u of frequency transition to %u kHz\n",
Dave Jonese4472cb2006-01-31 15:53:55 -0800271 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Mike Travis7a6aedf2008-03-25 15:06:53 -0700273 policy = per_cpu(cpufreq_cpu_data, freqs->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500277 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800278 * which is not equal to what the cpufreq core thinks is
279 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 */
281 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800282 if ((policy) && (policy->cpu == freqs->cpu) &&
283 (policy->cur) && (policy->cur != freqs->old)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200284 pr_debug("Warning: CPU frequency is"
Dave Jonese4472cb2006-01-31 15:53:55 -0800285 " %u, cpufreq assumed %u kHz.\n",
286 freqs->old, policy->cur);
287 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700290 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800291 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
293 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 case CPUFREQ_POSTCHANGE:
296 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200297 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
Thomas Renninger6f4f2722010-04-20 13:17:36 +0200298 (unsigned long)freqs->cpu);
299 trace_power_frequency(POWER_PSTATE, freqs->new, freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100300 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700301 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800302 CPUFREQ_POSTCHANGE, freqs);
Amar Singhal99055d52012-02-23 13:54:46 -0800303 if (likely(policy) && likely(policy->cpu == freqs->cpu)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800304 policy->cur = freqs->new;
Amar Singhal99055d52012-02-23 13:54:46 -0800305 sysfs_notify(&policy->kobj, NULL, "scaling_cur_freq");
306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 break;
308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800311/**
312 * cpufreq_notify_utilization - notify CPU userspace about CPU utilization
313 * change
314 *
315 * This function is called everytime the CPU load is evaluated by the
316 * ondemand governor. It notifies userspace of cpu load changes via sysfs.
317 */
318void cpufreq_notify_utilization(struct cpufreq_policy *policy,
319 unsigned int util)
320{
321 if (policy)
322 policy->util = util;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800324 if (policy->util >= MIN_CPU_UTIL_NOTIFY)
325 sysfs_notify(&policy->kobj, NULL, "cpu_utilization");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800327}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329/*********************************************************************
330 * SYSFS INTERFACE *
331 *********************************************************************/
332
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700333static struct cpufreq_governor *__find_governor(const char *str_governor)
334{
335 struct cpufreq_governor *t;
336
337 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
Dave Jones29464f22009-01-18 01:37:11 -0500338 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700339 return t;
340
341 return NULL;
342}
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344/**
345 * cpufreq_parse_governor - parse a governor string
346 */
Dave Jones905d77c2008-03-05 14:28:32 -0500347static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 struct cpufreq_governor **governor)
349{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700350 int err = -EINVAL;
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700353 goto out;
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (cpufreq_driver->setpolicy) {
356 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
357 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700358 err = 0;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530359 } else if (!strnicmp(str_governor, "powersave",
360 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700362 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700364 } else if (cpufreq_driver->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700366
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800367 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700368
369 t = __find_governor(str_governor);
370
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700371 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700372 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700373
Kees Cook1a8e1462011-05-04 08:38:56 -0700374 mutex_unlock(&cpufreq_governor_mutex);
375 ret = request_module("cpufreq_%s", str_governor);
376 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700377
Kees Cook1a8e1462011-05-04 08:38:56 -0700378 if (ret == 0)
379 t = __find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700380 }
381
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700382 if (t != NULL) {
383 *governor = t;
384 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700386
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800387 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
Dave Jones29464f22009-01-18 01:37:11 -0500389out:
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700390 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530395 * cpufreq_per_cpu_attr_read() / show_##file_name() -
396 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 *
398 * Write out information from cpufreq_driver->policy[cpu]; object must be
399 * "unsigned int".
400 */
401
Dave Jones32ee8c32006-02-28 00:43:23 -0500402#define show_one(file_name, object) \
403static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500404(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500405{ \
Dave Jones29464f22009-01-18 01:37:11 -0500406 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
409show_one(cpuinfo_min_freq, cpuinfo.min_freq);
410show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100411show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412show_one(scaling_min_freq, min);
413show_one(scaling_max_freq, max);
414show_one(scaling_cur_freq, cur);
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800415show_one(cpu_utilization, util);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530417static int __cpufreq_set_policy(struct cpufreq_policy *data,
418 struct cpufreq_policy *policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420/**
421 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
422 */
423#define store_one(file_name, object) \
424static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500425(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{ \
427 unsigned int ret = -EINVAL; \
428 struct cpufreq_policy new_policy; \
429 \
430 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
431 if (ret) \
432 return -EINVAL; \
433 \
Dave Jones29464f22009-01-18 01:37:11 -0500434 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (ret != 1) \
436 return -EINVAL; \
437 \
Thomas Renninger7970e082006-04-13 15:14:04 +0200438 ret = __cpufreq_set_policy(policy, &new_policy); \
439 policy->user_policy.object = policy->object; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 \
441 return ret ? ret : count; \
442}
443
Dave Jones29464f22009-01-18 01:37:11 -0500444store_one(scaling_min_freq, min);
445store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447/**
448 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
449 */
Dave Jones905d77c2008-03-05 14:28:32 -0500450static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
451 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800453 unsigned int cur_freq = __cpufreq_get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (!cur_freq)
455 return sprintf(buf, "<unknown>");
456 return sprintf(buf, "%u\n", cur_freq);
457}
458
459
460/**
461 * show_scaling_governor - show the current policy for the specified CPU
462 */
Dave Jones905d77c2008-03-05 14:28:32 -0500463static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Dave Jones29464f22009-01-18 01:37:11 -0500465 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return sprintf(buf, "powersave\n");
467 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
468 return sprintf(buf, "performance\n");
469 else if (policy->governor)
Dave Jones29464f22009-01-18 01:37:11 -0500470 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n",
471 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return -EINVAL;
473}
474
475
476/**
477 * store_scaling_governor - store policy for the specified CPU
478 */
Dave Jones905d77c2008-03-05 14:28:32 -0500479static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
480 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
482 unsigned int ret = -EINVAL;
483 char str_governor[16];
484 struct cpufreq_policy new_policy;
485
486 ret = cpufreq_get_policy(&new_policy, policy->cpu);
487 if (ret)
488 return ret;
489
Dave Jones29464f22009-01-18 01:37:11 -0500490 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (ret != 1)
492 return -EINVAL;
493
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530494 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
495 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return -EINVAL;
497
Thomas Renninger7970e082006-04-13 15:14:04 +0200498 /* Do not use cpufreq_set_policy here or the user_policy.max
499 will be wrongly overridden */
Thomas Renninger7970e082006-04-13 15:14:04 +0200500 ret = __cpufreq_set_policy(policy, &new_policy);
501
502 policy->user_policy.policy = policy->policy;
503 policy->user_policy.governor = policy->governor;
Thomas Renninger7970e082006-04-13 15:14:04 +0200504
Amar Singhal37d0b022012-05-25 14:40:05 -0700505 sysfs_notify(&policy->kobj, NULL, "scaling_governor");
506
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530507 if (ret)
508 return ret;
509 else
510 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
513/**
514 * show_scaling_driver - show the cpufreq driver currently loaded
515 */
Dave Jones905d77c2008-03-05 14:28:32 -0500516static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
519}
520
521/**
522 * show_scaling_available_governors - show the available CPUfreq governors
523 */
Dave Jones905d77c2008-03-05 14:28:32 -0500524static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
525 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
527 ssize_t i = 0;
528 struct cpufreq_governor *t;
529
530 if (!cpufreq_driver->target) {
531 i += sprintf(buf, "performance powersave");
532 goto out;
533 }
534
535 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
Dave Jones29464f22009-01-18 01:37:11 -0500536 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
537 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 goto out;
539 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
540 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500541out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 i += sprintf(&buf[i], "\n");
543 return i;
544}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700545
Rusty Russell835481d2009-01-04 05:18:06 -0800546static ssize_t show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
548 ssize_t i = 0;
549 unsigned int cpu;
550
Rusty Russell835481d2009-01-04 05:18:06 -0800551 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (i)
553 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
554 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
555 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500556 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558 i += sprintf(&buf[i], "\n");
559 return i;
560}
561
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700562/**
563 * show_related_cpus - show the CPUs affected by each transition even if
564 * hw coordination is in use
565 */
566static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
567{
Rusty Russell835481d2009-01-04 05:18:06 -0800568 if (cpumask_empty(policy->related_cpus))
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700569 return show_cpus(policy->cpus, buf);
570 return show_cpus(policy->related_cpus, buf);
571}
572
573/**
574 * show_affected_cpus - show the CPUs affected by each transition
575 */
576static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
577{
578 return show_cpus(policy->cpus, buf);
579}
580
Venki Pallipadi9e769882007-10-26 10:18:21 -0700581static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500582 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700583{
584 unsigned int freq = 0;
585 unsigned int ret;
586
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700587 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700588 return -EINVAL;
589
590 ret = sscanf(buf, "%u", &freq);
591 if (ret != 1)
592 return -EINVAL;
593
594 policy->governor->store_setspeed(policy, freq);
595
596 return count;
597}
598
599static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
600{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700601 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700602 return sprintf(buf, "<unsupported>\n");
603
604 return policy->governor->show_setspeed(policy, buf);
605}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Thomas Renningere2f74f32009-11-19 12:31:01 +0100607/**
608 * show_scaling_driver - show the current cpufreq HW/BIOS limitation
609 */
610static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
611{
612 unsigned int limit;
613 int ret;
614 if (cpufreq_driver->bios_limit) {
615 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
616 if (!ret)
617 return sprintf(buf, "%u\n", limit);
618 }
619 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
620}
621
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200622cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
623cpufreq_freq_attr_ro(cpuinfo_min_freq);
624cpufreq_freq_attr_ro(cpuinfo_max_freq);
625cpufreq_freq_attr_ro(cpuinfo_transition_latency);
626cpufreq_freq_attr_ro(scaling_available_governors);
627cpufreq_freq_attr_ro(scaling_driver);
628cpufreq_freq_attr_ro(scaling_cur_freq);
629cpufreq_freq_attr_ro(bios_limit);
630cpufreq_freq_attr_ro(related_cpus);
631cpufreq_freq_attr_ro(affected_cpus);
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800632cpufreq_freq_attr_ro(cpu_utilization);
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200633cpufreq_freq_attr_rw(scaling_min_freq);
634cpufreq_freq_attr_rw(scaling_max_freq);
635cpufreq_freq_attr_rw(scaling_governor);
636cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Dave Jones905d77c2008-03-05 14:28:32 -0500638static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 &cpuinfo_min_freq.attr,
640 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100641 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 &scaling_min_freq.attr,
643 &scaling_max_freq.attr,
644 &affected_cpus.attr,
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800645 &cpu_utilization.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700646 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 &scaling_governor.attr,
648 &scaling_driver.attr,
649 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700650 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 NULL
652};
653
Thomas Renninger8aa84ad2009-07-24 15:25:05 +0200654struct kobject *cpufreq_global_kobject;
655EXPORT_SYMBOL(cpufreq_global_kobject);
656
Dave Jones29464f22009-01-18 01:37:11 -0500657#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
658#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Dave Jones29464f22009-01-18 01:37:11 -0500660static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Dave Jones905d77c2008-03-05 14:28:32 -0500662 struct cpufreq_policy *policy = to_policy(kobj);
663 struct freq_attr *fattr = to_attr(attr);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500664 ssize_t ret = -EINVAL;
Stephen Boydf82ef512012-07-17 14:33:57 -0700665 policy = cpufreq_cpu_get_sysfs(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (!policy)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500667 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800668
669 if (lock_policy_rwsem_read(policy->cpu) < 0)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500670 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800671
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530672 if (fattr->show)
673 ret = fattr->show(policy, buf);
674 else
675 ret = -EIO;
676
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800677 unlock_policy_rwsem_read(policy->cpu);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500678fail:
Stephen Boydf82ef512012-07-17 14:33:57 -0700679 cpufreq_cpu_put_sysfs(policy);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500680no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return ret;
682}
683
Dave Jones905d77c2008-03-05 14:28:32 -0500684static ssize_t store(struct kobject *kobj, struct attribute *attr,
685 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Dave Jones905d77c2008-03-05 14:28:32 -0500687 struct cpufreq_policy *policy = to_policy(kobj);
688 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500689 ssize_t ret = -EINVAL;
Stephen Boydf82ef512012-07-17 14:33:57 -0700690 policy = cpufreq_cpu_get_sysfs(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (!policy)
Dave Jonesa07530b2008-03-05 14:22:25 -0500692 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800693
694 if (lock_policy_rwsem_write(policy->cpu) < 0)
Dave Jonesa07530b2008-03-05 14:22:25 -0500695 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800696
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530697 if (fattr->store)
698 ret = fattr->store(policy, buf, count);
699 else
700 ret = -EIO;
701
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800702 unlock_policy_rwsem_write(policy->cpu);
Dave Jonesa07530b2008-03-05 14:22:25 -0500703fail:
Stephen Boydf82ef512012-07-17 14:33:57 -0700704 cpufreq_cpu_put_sysfs(policy);
Dave Jonesa07530b2008-03-05 14:22:25 -0500705no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return ret;
707}
708
Dave Jones905d77c2008-03-05 14:28:32 -0500709static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Dave Jones905d77c2008-03-05 14:28:32 -0500711 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200712 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 complete(&policy->kobj_unregister);
714}
715
Emese Revfy52cf25d2010-01-19 02:58:23 +0100716static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 .show = show,
718 .store = store,
719};
720
721static struct kobj_type ktype_cpufreq = {
722 .sysfs_ops = &sysfs_ops,
723 .default_attrs = default_attrs,
724 .release = cpufreq_sysfs_release,
725};
726
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200727/*
728 * Returns:
729 * Negative: Failure
730 * 0: Success
731 * Positive: When we have a managed CPU and the sysfs got symlinked
732 */
Alex Chiangcf3289d2009-11-17 20:27:08 -0700733static int cpufreq_add_dev_policy(unsigned int cpu,
734 struct cpufreq_policy *policy,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800735 struct device *dev)
Dave Jonesecf7e462009-07-08 18:48:47 -0400736{
737 int ret = 0;
738#ifdef CONFIG_SMP
739 unsigned long flags;
740 unsigned int j;
Dave Jonesecf7e462009-07-08 18:48:47 -0400741#ifdef CONFIG_HOTPLUG_CPU
Dmitry Monakhove77b89f2009-10-05 00:38:55 +0400742 struct cpufreq_governor *gov;
743
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700744 gov = __find_governor(per_cpu(cpufreq_policy_save, cpu).gov);
Dmitry Monakhove77b89f2009-10-05 00:38:55 +0400745 if (gov) {
746 policy->governor = gov;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200747 pr_debug("Restoring governor %s for cpu %d\n",
Dave Jonesecf7e462009-07-08 18:48:47 -0400748 policy->governor->name, cpu);
749 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700750 if (per_cpu(cpufreq_policy_save, cpu).min) {
751 policy->min = per_cpu(cpufreq_policy_save, cpu).min;
752 policy->user_policy.min = policy->min;
753 }
754 if (per_cpu(cpufreq_policy_save, cpu).max) {
755 policy->max = per_cpu(cpufreq_policy_save, cpu).max;
756 policy->user_policy.max = policy->max;
757 }
758 pr_debug("Restoring CPU%d min %d and max %d\n",
759 cpu, policy->min, policy->max);
Dave Jonesecf7e462009-07-08 18:48:47 -0400760#endif
761
762 for_each_cpu(j, policy->cpus) {
763 struct cpufreq_policy *managed_policy;
764
765 if (cpu == j)
766 continue;
767
768 /* Check for existing affected CPUs.
769 * They may not be aware of it due to CPU Hotplug.
770 * cpufreq_cpu_put is called when the device is removed
771 * in __cpufreq_remove_dev()
772 */
773 managed_policy = cpufreq_cpu_get(j);
774 if (unlikely(managed_policy)) {
775
776 /* Set proper policy_cpu */
777 unlock_policy_rwsem_write(cpu);
Tejun Heof1625062009-10-29 22:34:13 +0900778 per_cpu(cpufreq_policy_cpu, cpu) = managed_policy->cpu;
Dave Jonesecf7e462009-07-08 18:48:47 -0400779
780 if (lock_policy_rwsem_write(cpu) < 0) {
781 /* Should not go through policy unlock path */
782 if (cpufreq_driver->exit)
783 cpufreq_driver->exit(policy);
784 cpufreq_cpu_put(managed_policy);
785 return -EBUSY;
786 }
787
788 spin_lock_irqsave(&cpufreq_driver_lock, flags);
789 cpumask_copy(managed_policy->cpus, policy->cpus);
Srivatsa Vaddagiride09f072013-03-01 16:16:27 -0800790 cpumask_and(managed_policy->cpus,
791 managed_policy->cpus, cpu_online_mask);
Dave Jonesecf7e462009-07-08 18:48:47 -0400792 per_cpu(cpufreq_cpu_data, cpu) = managed_policy;
793 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
794
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200795 pr_debug("CPU already managed, adding link\n");
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800796 ret = sysfs_create_link(&dev->kobj,
Dave Jonesecf7e462009-07-08 18:48:47 -0400797 &managed_policy->kobj,
798 "cpufreq");
799 if (ret)
800 cpufreq_cpu_put(managed_policy);
801 /*
802 * Success. We only needed to be added to the mask.
803 * Call driver->exit() because only the cpu parent of
804 * the kobj needed to call init().
805 */
806 if (cpufreq_driver->exit)
807 cpufreq_driver->exit(policy);
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200808
809 if (!ret)
810 return 1;
811 else
812 return ret;
Dave Jonesecf7e462009-07-08 18:48:47 -0400813 }
814 }
815#endif
816 return ret;
817}
818
819
Dave Jones19d6f7e2009-07-08 17:35:39 -0400820/* symlink affected CPUs */
Alex Chiangcf3289d2009-11-17 20:27:08 -0700821static int cpufreq_add_dev_symlink(unsigned int cpu,
822 struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400823{
824 unsigned int j;
825 int ret = 0;
826
827 for_each_cpu(j, policy->cpus) {
828 struct cpufreq_policy *managed_policy;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800829 struct device *cpu_dev;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400830
831 if (j == cpu)
832 continue;
833 if (!cpu_online(j))
834 continue;
835
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200836 pr_debug("CPU %u already managed, adding link\n", j);
Dave Jones19d6f7e2009-07-08 17:35:39 -0400837 managed_policy = cpufreq_cpu_get(cpu);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800838 cpu_dev = get_cpu_device(j);
839 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Dave Jones19d6f7e2009-07-08 17:35:39 -0400840 "cpufreq");
841 if (ret) {
842 cpufreq_cpu_put(managed_policy);
843 return ret;
844 }
845 }
846 return ret;
847}
848
Alex Chiangcf3289d2009-11-17 20:27:08 -0700849static int cpufreq_add_dev_interface(unsigned int cpu,
850 struct cpufreq_policy *policy,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800851 struct device *dev)
Dave Jones909a6942009-07-08 18:05:42 -0400852{
Dave Jonesecf7e462009-07-08 18:48:47 -0400853 struct cpufreq_policy new_policy;
Dave Jones909a6942009-07-08 18:05:42 -0400854 struct freq_attr **drv_attr;
855 unsigned long flags;
856 int ret = 0;
857 unsigned int j;
858
859 /* prepare interface data */
860 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800861 &dev->kobj, "cpufreq");
Dave Jones909a6942009-07-08 18:05:42 -0400862 if (ret)
863 return ret;
864
865 /* set up files for this cpu device */
866 drv_attr = cpufreq_driver->attr;
867 while ((drv_attr) && (*drv_attr)) {
868 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
869 if (ret)
870 goto err_out_kobj_put;
871 drv_attr++;
872 }
873 if (cpufreq_driver->get) {
874 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
875 if (ret)
876 goto err_out_kobj_put;
877 }
878 if (cpufreq_driver->target) {
879 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
880 if (ret)
881 goto err_out_kobj_put;
882 }
Thomas Renningere2f74f32009-11-19 12:31:01 +0100883 if (cpufreq_driver->bios_limit) {
884 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
885 if (ret)
886 goto err_out_kobj_put;
887 }
Dave Jones909a6942009-07-08 18:05:42 -0400888
889 spin_lock_irqsave(&cpufreq_driver_lock, flags);
890 for_each_cpu(j, policy->cpus) {
Julia Lawallbec037a2010-08-05 22:23:00 +0200891 if (!cpu_online(j))
892 continue;
Dave Jones909a6942009-07-08 18:05:42 -0400893 per_cpu(cpufreq_cpu_data, j) = policy;
Tejun Heof1625062009-10-29 22:34:13 +0900894 per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
Dave Jones909a6942009-07-08 18:05:42 -0400895 }
896 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
897
898 ret = cpufreq_add_dev_symlink(cpu, policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400899 if (ret)
900 goto err_out_kobj_put;
901
902 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
903 /* assure that the starting sequence is run in __cpufreq_set_policy */
904 policy->governor = NULL;
905
906 /* set default policy */
907 ret = __cpufreq_set_policy(policy, &new_policy);
908 policy->user_policy.policy = policy->policy;
909 policy->user_policy.governor = policy->governor;
910
911 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200912 pr_debug("setting policy failed\n");
Dave Jonesecf7e462009-07-08 18:48:47 -0400913 if (cpufreq_driver->exit)
914 cpufreq_driver->exit(policy);
915 }
Dave Jones909a6942009-07-08 18:05:42 -0400916 return ret;
917
918err_out_kobj_put:
919 kobject_put(&policy->kobj);
920 wait_for_completion(&policy->kobj_unregister);
921 return ret;
922}
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925/**
926 * cpufreq_add_dev - add a CPU device
927 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500928 * Adds the cpufreq interface for a CPU device.
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400929 *
930 * The Oracle says: try running cpufreq registration/unregistration concurrently
931 * with with cpu hotplugging and all hell will break loose. Tried to clean this
932 * mess up, but more thorough testing is needed. - Mathieu
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800934static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800936 unsigned int cpu = dev->id;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500937 int ret = 0, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 unsigned long flags;
940 unsigned int j;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500941#ifdef CONFIG_HOTPLUG_CPU
942 int sibling;
943#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Ashok Rajc32b6b82005-10-30 14:59:54 -0800945 if (cpu_is_offline(cpu))
946 return 0;
947
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200948 pr_debug("adding CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950#ifdef CONFIG_SMP
951 /* check whether a different CPU already registered this
952 * CPU because it is in the same boat. */
953 policy = cpufreq_cpu_get(cpu);
954 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -0500955 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return 0;
957 }
958#endif
959
960 if (!try_module_get(cpufreq_driver->owner)) {
961 ret = -EINVAL;
962 goto module_out;
963 }
964
Dave Jones059019a2009-07-08 16:30:03 -0400965 ret = -ENOMEM;
Dave Jonese98df502005-10-20 15:17:43 -0700966 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
Dave Jones059019a2009-07-08 16:30:03 -0400967 if (!policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 goto nomem_out;
Dave Jones059019a2009-07-08 16:30:03 -0400969
970 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400971 goto err_free_policy;
Dave Jones059019a2009-07-08 16:30:03 -0400972
973 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400974 goto err_free_cpumask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 policy->cpu = cpu;
Rusty Russell835481d2009-01-04 05:18:06 -0800977 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800979 /* Initially set CPU itself as the policy_cpu */
Tejun Heof1625062009-10-29 22:34:13 +0900980 per_cpu(cpufreq_policy_cpu, cpu) = cpu;
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400981 ret = (lock_policy_rwsem_write(cpu) < 0);
982 WARN_ON(ret);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 init_completion(&policy->kobj_unregister);
David Howells65f27f32006-11-22 14:55:48 +0000985 INIT_WORK(&policy->update, handle_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Thomas Renninger8122c6c2007-10-02 13:28:09 -0700987 /* Set governor before ->init, so that driver could check it */
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500988#ifdef CONFIG_HOTPLUG_CPU
989 for_each_online_cpu(sibling) {
990 struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
991 if (cp && cp->governor &&
992 (cpumask_test_cpu(cpu, cp->related_cpus))) {
993 policy->governor = cp->governor;
994 found = 1;
995 break;
996 }
997 }
998#endif
999 if (!found)
1000 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 /* call driver. From then on the cpufreq must be able
1002 * to accept all calls to ->verify and ->setpolicy for this CPU
1003 */
1004 ret = cpufreq_driver->init(policy);
1005 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001006 pr_debug("initialization failed\n");
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001007 goto err_unlock_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 }
Mike Chan187d9f42008-12-04 12:19:17 -08001009 policy->user_policy.min = policy->min;
1010 policy->user_policy.max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Thomas Renningera1531ac2008-07-29 22:32:58 -07001012 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1013 CPUFREQ_START, policy);
1014
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001015 ret = cpufreq_add_dev_policy(cpu, policy, dev);
Thomas Renninger4bfa0422009-07-24 15:25:03 +02001016 if (ret) {
1017 if (ret > 0)
1018 /* This is a managed cpu, symlink created,
1019 exit with 0 */
1020 ret = 0;
Dave Jonesecf7e462009-07-08 18:48:47 -04001021 goto err_unlock_policy;
Thomas Renninger4bfa0422009-07-24 15:25:03 +02001022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001024 ret = cpufreq_add_dev_interface(cpu, policy, dev);
Dave Jones19d6f7e2009-07-08 17:35:39 -04001025 if (ret)
1026 goto err_out_unregister;
Dave Jones8ff69732006-03-05 03:37:23 -05001027
Lothar Waßmanndca02612008-05-29 17:54:52 +02001028 unlock_policy_rwsem_write(cpu);
1029
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001030 kobject_uevent(&policy->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 module_put(cpufreq_driver->owner);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001032 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return 0;
1035
1036
1037err_out_unregister:
1038 spin_lock_irqsave(&cpufreq_driver_lock, flags);
Rusty Russell835481d2009-01-04 05:18:06 -08001039 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -07001040 per_cpu(cpufreq_cpu_data, j) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1042
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -08001043 kobject_put(&policy->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 wait_for_completion(&policy->kobj_unregister);
1045
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001046err_unlock_policy:
Dave Jones45709112008-03-05 14:07:34 -05001047 unlock_policy_rwsem_write(cpu);
Xiaotian Fengcad70a62010-07-20 20:11:02 +08001048 free_cpumask_var(policy->related_cpus);
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001049err_free_cpumask:
1050 free_cpumask_var(policy->cpus);
1051err_free_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 kfree(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053nomem_out:
1054 module_put(cpufreq_driver->owner);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001055module_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 return ret;
1057}
1058
1059
1060/**
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001061 * __cpufreq_remove_dev - remove a CPU device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 *
1063 * Removes the cpufreq interface for a CPU device.
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001064 * Caller should already have policy_rwsem in write mode for this CPU.
1065 * This routine frees the rwsem before returning.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001067static int __cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001069 unsigned int cpu = dev->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 unsigned long flags;
1071 struct cpufreq_policy *data;
Amerigo Wang499bca92010-03-04 03:23:46 -05001072 struct kobject *kobj;
1073 struct completion *cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074#ifdef CONFIG_SMP
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001075 struct device *cpu_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 unsigned int j;
1077#endif
1078
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001079 pr_debug("unregistering CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 spin_lock_irqsave(&cpufreq_driver_lock, flags);
Mike Travis7a6aedf2008-03-25 15:06:53 -07001082 data = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084 if (!data) {
1085 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001086 unlock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 return -EINVAL;
1088 }
Mike Travis7a6aedf2008-03-25 15:06:53 -07001089 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091
1092#ifdef CONFIG_SMP
1093 /* if this isn't the CPU which is the parent of the kobj, we
Dave Jones32ee8c32006-02-28 00:43:23 -05001094 * only need to unlink, put and exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 */
1096 if (unlikely(cpu != data->cpu)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001097 pr_debug("removing link\n");
Rusty Russell835481d2009-01-04 05:18:06 -08001098 cpumask_clear_cpu(cpu, data->cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001100 kobj = &dev->kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 cpufreq_cpu_put(data);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001102 unlock_policy_rwsem_write(cpu);
Amerigo Wang499bca92010-03-04 03:23:46 -05001103 sysfs_remove_link(kobj, "cpufreq");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 return 0;
1105 }
1106#endif
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108#ifdef CONFIG_SMP
Thomas Renninger084f3492007-07-09 11:35:28 -07001109
1110#ifdef CONFIG_HOTPLUG_CPU
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001111 strncpy(per_cpu(cpufreq_policy_save, cpu).gov, data->governor->name,
Dmitry Monakhove77b89f2009-10-05 00:38:55 +04001112 CPUFREQ_NAME_LEN);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001113 per_cpu(cpufreq_policy_save, cpu).min = data->min;
1114 per_cpu(cpufreq_policy_save, cpu).max = data->max;
1115 pr_debug("Saving CPU%d policy min %d and max %d\n",
1116 cpu, data->min, data->max);
Thomas Renninger084f3492007-07-09 11:35:28 -07001117#endif
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 /* if we have other CPUs still registered, we need to unlink them,
1120 * or else wait_for_completion below will lock up. Clean the
Mike Travis7a6aedf2008-03-25 15:06:53 -07001121 * per_cpu(cpufreq_cpu_data) while holding the lock, and remove
1122 * the sysfs links afterwards.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 */
Rusty Russell835481d2009-01-04 05:18:06 -08001124 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1125 for_each_cpu(j, data->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (j == cpu)
1127 continue;
Mike Travis7a6aedf2008-03-25 15:06:53 -07001128 per_cpu(cpufreq_cpu_data, j) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
1130 }
1131
1132 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1133
Rusty Russell835481d2009-01-04 05:18:06 -08001134 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1135 for_each_cpu(j, data->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 if (j == cpu)
1137 continue;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001138 pr_debug("removing link for cpu %u\n", j);
Thomas Renninger084f3492007-07-09 11:35:28 -07001139#ifdef CONFIG_HOTPLUG_CPU
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001140 strncpy(per_cpu(cpufreq_policy_save, j).gov,
Dmitry Monakhove77b89f2009-10-05 00:38:55 +04001141 data->governor->name, CPUFREQ_NAME_LEN);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001142 per_cpu(cpufreq_policy_save, j).min = data->min;
1143 per_cpu(cpufreq_policy_save, j).max = data->max;
1144 pr_debug("Saving CPU%d policy min %d and max %d\n",
1145 j, data->min, data->max);
Thomas Renninger084f3492007-07-09 11:35:28 -07001146#endif
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001147 cpu_dev = get_cpu_device(j);
1148 kobj = &cpu_dev->kobj;
Amerigo Wang499bca92010-03-04 03:23:46 -05001149 unlock_policy_rwsem_write(cpu);
1150 sysfs_remove_link(kobj, "cpufreq");
1151 lock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 cpufreq_cpu_put(data);
1153 }
1154 }
1155#else
1156 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1157#endif
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 if (cpufreq_driver->target)
1160 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001161
Amerigo Wang499bca92010-03-04 03:23:46 -05001162 kobj = &data->kobj;
1163 cmp = &data->kobj_unregister;
1164 unlock_policy_rwsem_write(cpu);
1165 kobject_put(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 /* we need to make sure that the underlying kobj is actually
Dave Jones32ee8c32006-02-28 00:43:23 -05001168 * not referenced anymore by anybody before we proceed with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 * unloading.
1170 */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001171 pr_debug("waiting for dropping of refcount\n");
Amerigo Wang499bca92010-03-04 03:23:46 -05001172 wait_for_completion(cmp);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001173 pr_debug("wait complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Amerigo Wang499bca92010-03-04 03:23:46 -05001175 lock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if (cpufreq_driver->exit)
1177 cpufreq_driver->exit(data);
venkatesh.pallipadi@intel.com7d26e2d2009-07-02 17:08:30 -07001178 unlock_policy_rwsem_write(cpu);
1179
Jacob Shin27ecddc2011-04-27 13:32:11 -05001180#ifdef CONFIG_HOTPLUG_CPU
1181 /* when the CPU which is the parent of the kobj is hotplugged
1182 * offline, check for siblings, and create cpufreq sysfs interface
1183 * and symlinks
1184 */
1185 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1186 /* first sibling now owns the new sysfs dir */
1187 cpumask_clear_cpu(cpu, data->cpus);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001188 cpufreq_add_dev(get_cpu_device(cpumask_first(data->cpus)), NULL);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001189
1190 /* finally remove our own symlink */
1191 lock_policy_rwsem_write(cpu);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001192 __cpufreq_remove_dev(dev, sif);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001193 }
1194#endif
1195
Rusty Russell835481d2009-01-04 05:18:06 -08001196 free_cpumask_var(data->related_cpus);
1197 free_cpumask_var(data->cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 kfree(data);
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 return 0;
1201}
1202
1203
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001204static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001205{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001206 unsigned int cpu = dev->id;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001207 int retval;
Venki Pallipadiec282972007-03-26 12:03:19 -07001208
1209 if (cpu_is_offline(cpu))
1210 return 0;
1211
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001212 if (unlikely(lock_policy_rwsem_write(cpu)))
1213 BUG();
1214
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001215 retval = __cpufreq_remove_dev(dev, sif);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001216 return retval;
1217}
1218
1219
David Howells65f27f32006-11-22 14:55:48 +00001220static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
David Howells65f27f32006-11-22 14:55:48 +00001222 struct cpufreq_policy *policy =
1223 container_of(work, struct cpufreq_policy, update);
1224 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001225 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 cpufreq_update_policy(cpu);
1227}
1228
1229/**
1230 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
1231 * @cpu: cpu number
1232 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1233 * @new_freq: CPU frequency the CPU actually runs at
1234 *
Dave Jones29464f22009-01-18 01:37:11 -05001235 * We adjust to current frequency first, and need to clean up later.
1236 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301238static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1239 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
1241 struct cpufreq_freqs freqs;
1242
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001243 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1245
1246 freqs.cpu = cpu;
1247 freqs.old = old_freq;
1248 freqs.new = new_freq;
1249 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
1250 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
1251}
1252
1253
Dave Jones32ee8c32006-02-28 00:43:23 -05001254/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301255 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001256 * @cpu: CPU number
1257 *
1258 * This is the last known freq, without actually getting it from the driver.
1259 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1260 */
1261unsigned int cpufreq_quick_get(unsigned int cpu)
1262{
1263 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301264 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001265
1266 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301267 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001268 cpufreq_cpu_put(policy);
1269 }
1270
Dave Jones4d34a672008-02-07 16:33:49 -05001271 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001272}
1273EXPORT_SYMBOL(cpufreq_quick_get);
1274
Jesse Barnes3d737102011-06-28 10:59:12 -07001275/**
1276 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1277 * @cpu: CPU number
1278 *
1279 * Just return the max possible frequency for a given CPU.
1280 */
1281unsigned int cpufreq_quick_get_max(unsigned int cpu)
1282{
1283 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1284 unsigned int ret_freq = 0;
1285
1286 if (policy) {
1287 ret_freq = policy->max;
1288 cpufreq_cpu_put(policy);
1289 }
1290
1291 return ret_freq;
1292}
1293EXPORT_SYMBOL(cpufreq_quick_get_max);
1294
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001295
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001296static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001298 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301299 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001302 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301304 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301306 if (ret_freq && policy->cur &&
1307 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1308 /* verify no discrepancy between actual and
1309 saved value exists */
1310 if (unlikely(ret_freq != policy->cur)) {
1311 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 schedule_work(&policy->update);
1313 }
1314 }
1315
Dave Jones4d34a672008-02-07 16:33:49 -05001316 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001317}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001319/**
1320 * cpufreq_get - get the current CPU frequency (in kHz)
1321 * @cpu: CPU number
1322 *
1323 * Get the CPU current (static) CPU frequency
1324 */
1325unsigned int cpufreq_get(unsigned int cpu)
1326{
1327 unsigned int ret_freq = 0;
1328 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1329
1330 if (!policy)
1331 goto out;
1332
1333 if (unlikely(lock_policy_rwsem_read(cpu)))
1334 goto out_policy;
1335
1336 ret_freq = __cpufreq_get(cpu);
1337
1338 unlock_policy_rwsem_read(cpu);
1339
1340out_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 cpufreq_cpu_put(policy);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001342out:
Dave Jones4d34a672008-02-07 16:33:49 -05001343 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345EXPORT_SYMBOL(cpufreq_get);
1346
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001347static struct subsys_interface cpufreq_interface = {
1348 .name = "cpufreq",
1349 .subsys = &cpu_subsys,
1350 .add_dev = cpufreq_add_dev,
1351 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001352};
1353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001356 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1357 *
1358 * This function is only executed for the boot processor. The other CPUs
1359 * have been put offline by means of CPU hotplug.
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001360 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001361static int cpufreq_bp_suspend(void)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001362{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301363 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001364
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001365 int cpu = smp_processor_id();
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001366 struct cpufreq_policy *cpu_policy;
1367
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001368 pr_debug("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001369
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001370 /* If there's no policy for the boot CPU, we have nothing to do. */
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001371 cpu_policy = cpufreq_cpu_get(cpu);
1372 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001373 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001374
1375 if (cpufreq_driver->suspend) {
Rafael J. Wysocki7ca64e22011-03-10 21:13:05 +01001376 ret = cpufreq_driver->suspend(cpu_policy);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001377 if (ret)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001378 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1379 "step on CPU %u\n", cpu_policy->cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001380 }
1381
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001382 cpufreq_cpu_put(cpu_policy);
Dave Jonesc9060492008-02-07 16:32:18 -05001383 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001384}
1385
1386/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001387 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 *
1389 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001390 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1391 * restored. It will verify that the current freq is in sync with
1392 * what we believe it to be. This is a bit later than when it
1393 * should be, but nonethteless it's better than calling
1394 * cpufreq_driver->get() here which might re-enable interrupts...
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001395 *
1396 * This function is only executed for the boot CPU. The other CPUs have not
1397 * been turned on yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001399static void cpufreq_bp_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301401 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001402
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001403 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 struct cpufreq_policy *cpu_policy;
1405
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001406 pr_debug("resuming cpu %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001408 /* If there's no policy for the boot CPU, we have nothing to do. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 cpu_policy = cpufreq_cpu_get(cpu);
1410 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001411 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
1413 if (cpufreq_driver->resume) {
1414 ret = cpufreq_driver->resume(cpu_policy);
1415 if (ret) {
1416 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1417 "step on CPU %u\n", cpu_policy->cpu);
Dave Jonesc9060492008-02-07 16:32:18 -05001418 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
1420 }
1421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 schedule_work(&cpu_policy->update);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001423
Dave Jonesc9060492008-02-07 16:32:18 -05001424fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426}
1427
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001428static struct syscore_ops cpufreq_syscore_ops = {
1429 .suspend = cpufreq_bp_suspend,
1430 .resume = cpufreq_bp_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431};
1432
1433
1434/*********************************************************************
1435 * NOTIFIER LISTS INTERFACE *
1436 *********************************************************************/
1437
1438/**
1439 * cpufreq_register_notifier - register a driver with cpufreq
1440 * @nb: notifier function to register
1441 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1442 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001443 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 * are notified about clock rate changes (once before and once after
1445 * the transition), or a list of drivers that are notified about
1446 * changes in cpufreq policy.
1447 *
1448 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001449 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 */
1451int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1452{
1453 int ret;
1454
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001455 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 switch (list) {
1458 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001459 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001460 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 break;
1462 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001463 ret = blocking_notifier_chain_register(
1464 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 break;
1466 default:
1467 ret = -EINVAL;
1468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
1470 return ret;
1471}
1472EXPORT_SYMBOL(cpufreq_register_notifier);
1473
1474
1475/**
1476 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1477 * @nb: notifier block to be unregistered
1478 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1479 *
1480 * Remove a driver from the CPU frequency notifier list.
1481 *
1482 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001483 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 */
1485int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1486{
1487 int ret;
1488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 switch (list) {
1490 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001491 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001492 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 break;
1494 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001495 ret = blocking_notifier_chain_unregister(
1496 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 break;
1498 default:
1499 ret = -EINVAL;
1500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
1502 return ret;
1503}
1504EXPORT_SYMBOL(cpufreq_unregister_notifier);
1505
1506
1507/*********************************************************************
1508 * GOVERNORS *
1509 *********************************************************************/
1510
1511
1512int __cpufreq_driver_target(struct cpufreq_policy *policy,
1513 unsigned int target_freq,
1514 unsigned int relation)
1515{
1516 int retval = -EINVAL;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001517
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001518 if (cpufreq_disabled())
1519 return -ENODEV;
1520
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001521 pr_debug("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 target_freq, relation);
1523 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1524 retval = cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 return retval;
1527}
1528EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530int cpufreq_driver_target(struct cpufreq_policy *policy,
1531 unsigned int target_freq,
1532 unsigned int relation)
1533{
Julia Lawallf1829e42008-07-25 22:44:53 +02001534 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536 policy = cpufreq_cpu_get(policy->cpu);
1537 if (!policy)
Julia Lawallf1829e42008-07-25 22:44:53 +02001538 goto no_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001540 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
Julia Lawallf1829e42008-07-25 22:44:53 +02001541 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
1543 ret = __cpufreq_driver_target(policy, target_freq, relation);
1544
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001545 unlock_policy_rwsem_write(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Julia Lawallf1829e42008-07-25 22:44:53 +02001547fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 cpufreq_cpu_put(policy);
Julia Lawallf1829e42008-07-25 22:44:53 +02001549no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 return ret;
1551}
1552EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1553
venkatesh.pallipadi@intel.combf0b90e2008-08-04 11:59:07 -07001554int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu)
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001555{
1556 int ret = 0;
1557
1558 policy = cpufreq_cpu_get(policy->cpu);
1559 if (!policy)
1560 return -EINVAL;
1561
venkatesh.pallipadi@intel.combf0b90e2008-08-04 11:59:07 -07001562 if (cpu_online(cpu) && cpufreq_driver->getavg)
1563 ret = cpufreq_driver->getavg(policy, cpu);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001564
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001565 cpufreq_cpu_put(policy);
1566 return ret;
1567}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001568EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001569
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001570/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001571 * when "event" is CPUFREQ_GOV_LIMITS
1572 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301574static int __cpufreq_governor(struct cpufreq_policy *policy,
1575 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
Dave Jonescc993ca2005-07-28 09:43:56 -07001577 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001578
1579 /* Only must be defined when default governor is known to have latency
1580 restrictions, like e.g. conservative or ondemand.
1581 That this is the case is already ensured in Kconfig
1582 */
1583#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1584 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1585#else
1586 struct cpufreq_governor *gov = NULL;
1587#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001588
1589 if (policy->governor->max_transition_latency &&
1590 policy->cpuinfo.transition_latency >
1591 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07001592 if (!gov)
1593 return -EINVAL;
1594 else {
1595 printk(KERN_WARNING "%s governor failed, too long"
1596 " transition latency of HW, fallback"
1597 " to %s governor\n",
1598 policy->governor->name,
1599 gov->name);
1600 policy->governor = gov;
1601 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604 if (!try_module_get(policy->governor->owner))
1605 return -EINVAL;
1606
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001607 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301608 policy->cpu, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 ret = policy->governor->governor(policy, event);
1610
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301611 /* we keep one module reference alive for
1612 each CPU governed by this CPU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 if ((event != CPUFREQ_GOV_START) || ret)
1614 module_put(policy->governor->owner);
1615 if ((event == CPUFREQ_GOV_STOP) && !ret)
1616 module_put(policy->governor->owner);
1617
1618 return ret;
1619}
1620
1621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622int cpufreq_register_governor(struct cpufreq_governor *governor)
1623{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001624 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626 if (!governor)
1627 return -EINVAL;
1628
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001629 if (cpufreq_disabled())
1630 return -ENODEV;
1631
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001632 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001633
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001634 err = -EBUSY;
1635 if (__find_governor(governor->name) == NULL) {
1636 err = 0;
1637 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Dave Jones32ee8c32006-02-28 00:43:23 -05001640 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001641 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642}
1643EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1644
1645
1646void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1647{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001648#ifdef CONFIG_HOTPLUG_CPU
1649 int cpu;
1650#endif
1651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 if (!governor)
1653 return;
1654
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001655 if (cpufreq_disabled())
1656 return;
1657
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001658#ifdef CONFIG_HOTPLUG_CPU
1659 for_each_present_cpu(cpu) {
1660 if (cpu_online(cpu))
1661 continue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001662 if (!strcmp(per_cpu(cpufreq_policy_save, cpu).gov,
1663 governor->name))
1664 strcpy(per_cpu(cpufreq_policy_save, cpu).gov, "\0");
1665 per_cpu(cpufreq_policy_save, cpu).min = 0;
1666 per_cpu(cpufreq_policy_save, cpu).max = 0;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001667 }
1668#endif
1669
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001670 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001672 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 return;
1674}
1675EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1676
1677
1678
1679/*********************************************************************
1680 * POLICY INTERFACE *
1681 *********************************************************************/
1682
1683/**
1684 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05001685 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1686 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 *
1688 * Reads the current cpufreq policy.
1689 */
1690int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1691{
1692 struct cpufreq_policy *cpu_policy;
1693 if (!policy)
1694 return -EINVAL;
1695
1696 cpu_policy = cpufreq_cpu_get(cpu);
1697 if (!cpu_policy)
1698 return -EINVAL;
1699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
1702 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 return 0;
1704}
1705EXPORT_SYMBOL(cpufreq_get_policy);
1706
1707
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001708/*
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301709 * data : current policy.
1710 * policy : policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001711 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301712static int __cpufreq_set_policy(struct cpufreq_policy *data,
1713 struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
1715 int ret = 0;
1716
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001717 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 policy->min, policy->max);
1719
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301720 memcpy(&policy->cpuinfo, &data->cpuinfo,
1721 sizeof(struct cpufreq_cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Yi Yang53391fa2008-01-30 13:33:34 +01001723 if (policy->min > data->max || policy->max < data->min) {
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02001724 ret = -EINVAL;
1725 goto error_out;
1726 }
1727
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 /* verify the cpu speed can be set within this limit */
1729 ret = cpufreq_driver->verify(policy);
1730 if (ret)
1731 goto error_out;
1732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001734 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1735 CPUFREQ_ADJUST, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001738 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1739 CPUFREQ_INCOMPATIBLE, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
1741 /* verify the cpu speed can be set within this limit,
1742 which might be different to the first one */
1743 ret = cpufreq_driver->verify(policy);
Alan Sterne041c682006-03-27 01:16:30 -08001744 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
1747 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001748 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1749 CPUFREQ_NOTIFY, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Dave Jones7d5e3502006-02-02 17:03:42 -05001751 data->min = policy->min;
1752 data->max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001754 pr_debug("new min and max freqs are %u - %u kHz\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301755 data->min, data->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 if (cpufreq_driver->setpolicy) {
1758 data->policy = policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001759 pr_debug("setting range\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 ret = cpufreq_driver->setpolicy(policy);
1761 } else {
1762 if (policy->governor != data->governor) {
1763 /* save old, working values */
1764 struct cpufreq_governor *old_gov = data->governor;
1765
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001766 pr_debug("governor switch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
1768 /* end old governor */
Andrej Gelenbergffe62752010-05-14 15:15:58 -07001769 if (data->governor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1771
1772 /* start new governor */
1773 data->governor = policy->governor;
1774 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1775 /* new governor failed, so re-start old one */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001776 pr_debug("starting governor %s failed\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301777 data->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 if (old_gov) {
1779 data->governor = old_gov;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301780 __cpufreq_governor(data,
1781 CPUFREQ_GOV_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 }
1783 ret = -EINVAL;
1784 goto error_out;
1785 }
1786 /* might be a policy change, too, so fall through */
1787 }
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001788 pr_debug("governor: change or update limits\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1790 }
1791
Dave Jones7d5e3502006-02-02 17:03:42 -05001792error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 return ret;
1794}
1795
1796/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1798 * @cpu: CPU which shall be re-evaluated
1799 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001800 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 * at different times.
1802 */
1803int cpufreq_update_policy(unsigned int cpu)
1804{
1805 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1806 struct cpufreq_policy policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02001807 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Julia Lawallf1829e42008-07-25 22:44:53 +02001809 if (!data) {
1810 ret = -ENODEV;
1811 goto no_policy;
1812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Julia Lawallf1829e42008-07-25 22:44:53 +02001814 if (unlikely(lock_policy_rwsem_write(cpu))) {
1815 ret = -EINVAL;
1816 goto fail;
1817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001819 pr_debug("updating policy for CPU %u\n", cpu);
Dave Jones7d5e3502006-02-02 17:03:42 -05001820 memcpy(&policy, data, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 policy.min = data->user_policy.min;
1822 policy.max = data->user_policy.max;
1823 policy.policy = data->user_policy.policy;
1824 policy.governor = data->user_policy.governor;
1825
Thomas Renninger0961dd02006-01-26 18:46:33 +01001826 /* BIOS might change freq behind our back
1827 -> ask driver for current freq and notify governors about a change */
1828 if (cpufreq_driver->get) {
1829 policy.cur = cpufreq_driver->get(cpu);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001830 if (!data->cur) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001831 pr_debug("Driver did not initialize current freq");
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001832 data->cur = policy.cur;
1833 } else {
1834 if (data->cur != policy.cur)
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301835 cpufreq_out_of_sync(cpu, data->cur,
1836 policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001837 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01001838 }
1839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 ret = __cpufreq_set_policy(data, &policy);
1841
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001842 unlock_policy_rwsem_write(cpu);
1843
Julia Lawallf1829e42008-07-25 22:44:53 +02001844fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 cpufreq_cpu_put(data);
Julia Lawallf1829e42008-07-25 22:44:53 +02001846no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 return ret;
1848}
1849EXPORT_SYMBOL(cpufreq_update_policy);
1850
Satyam Sharmadd184a02007-10-02 13:28:14 -07001851static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08001852 unsigned long action, void *hcpu)
1853{
1854 unsigned int cpu = (unsigned long)hcpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001855 struct device *dev;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001856
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001857 dev = get_cpu_device(cpu);
1858 if (dev) {
Ashok Rajc32b6b82005-10-30 14:59:54 -08001859 switch (action) {
1860 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001861 case CPU_ONLINE_FROZEN:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001862 cpufreq_add_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001863 break;
1864 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001865 case CPU_DOWN_PREPARE_FROZEN:
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001866 if (unlikely(lock_policy_rwsem_write(cpu)))
1867 BUG();
1868
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001869 __cpufreq_remove_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001870 break;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001871 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001872 case CPU_DOWN_FAILED_FROZEN:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001873 cpufreq_add_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001874 break;
1875 }
1876 }
1877 return NOTIFY_OK;
1878}
1879
Neal Buckendahl9c36f742010-06-22 22:02:44 -05001880static struct notifier_block __refdata cpufreq_cpu_notifier = {
Ashok Rajc32b6b82005-10-30 14:59:54 -08001881 .notifier_call = cpufreq_cpu_callback,
1882};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
1884/*********************************************************************
1885 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1886 *********************************************************************/
1887
1888/**
1889 * cpufreq_register_driver - register a CPU Frequency driver
1890 * @driver_data: A struct cpufreq_driver containing the values#
1891 * submitted by the CPU Frequency driver.
1892 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001893 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05001895 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 *
1897 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001898int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
1900 unsigned long flags;
1901 int ret;
1902
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001903 if (cpufreq_disabled())
1904 return -ENODEV;
1905
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 if (!driver_data || !driver_data->verify || !driver_data->init ||
1907 ((!driver_data->setpolicy) && (!driver_data->target)))
1908 return -EINVAL;
1909
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001910 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
1912 if (driver_data->setpolicy)
1913 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1914
1915 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1916 if (cpufreq_driver) {
1917 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1918 return -EBUSY;
1919 }
1920 cpufreq_driver = driver_data;
1921 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1922
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001923 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001924 if (ret)
1925 goto err_null_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001927 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 int i;
1929 ret = -ENODEV;
1930
1931 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07001932 for (i = 0; i < nr_cpu_ids; i++)
1933 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07001935 break;
1936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
1938 /* if all ->init() calls failed, unregister */
1939 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001940 pr_debug("no CPU initialized for driver %s\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301941 driver_data->name);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001942 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 }
1944 }
1945
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001946 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001947 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001949 return 0;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001950err_if_unreg:
1951 subsys_interface_unregister(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001952err_null_driver:
1953 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1954 cpufreq_driver = NULL;
1955 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05001956 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957}
1958EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1959
1960
1961/**
1962 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1963 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001964 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 * the right to do so, i.e. if you have succeeded in initialising before!
1966 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1967 * currently not initialised.
1968 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001969int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
1971 unsigned long flags;
1972
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001973 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001976 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001978 subsys_interface_unregister(&cpufreq_interface);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001979 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
1981 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1982 cpufreq_driver = NULL;
1983 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1984
1985 return 0;
1986}
1987EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001988
1989static int __init cpufreq_core_init(void)
1990{
1991 int cpu;
1992
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001993 if (cpufreq_disabled())
1994 return -ENODEV;
1995
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001996 for_each_possible_cpu(cpu) {
Tejun Heof1625062009-10-29 22:34:13 +09001997 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001998 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
1999 }
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002000
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002001 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002002 BUG_ON(!cpufreq_global_kobject);
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01002003 register_syscore_ops(&cpufreq_syscore_ops);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002004
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002005 return 0;
2006}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002007core_initcall(cpufreq_core_init);