blob: be713478984be2d165ee45e0f31e4754654e6da8 [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);
790 per_cpu(cpufreq_cpu_data, cpu) = managed_policy;
791 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
792
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200793 pr_debug("CPU already managed, adding link\n");
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800794 ret = sysfs_create_link(&dev->kobj,
Dave Jonesecf7e462009-07-08 18:48:47 -0400795 &managed_policy->kobj,
796 "cpufreq");
797 if (ret)
798 cpufreq_cpu_put(managed_policy);
799 /*
800 * Success. We only needed to be added to the mask.
801 * Call driver->exit() because only the cpu parent of
802 * the kobj needed to call init().
803 */
804 if (cpufreq_driver->exit)
805 cpufreq_driver->exit(policy);
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200806
807 if (!ret)
808 return 1;
809 else
810 return ret;
Dave Jonesecf7e462009-07-08 18:48:47 -0400811 }
812 }
813#endif
814 return ret;
815}
816
817
Dave Jones19d6f7e2009-07-08 17:35:39 -0400818/* symlink affected CPUs */
Alex Chiangcf3289d2009-11-17 20:27:08 -0700819static int cpufreq_add_dev_symlink(unsigned int cpu,
820 struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400821{
822 unsigned int j;
823 int ret = 0;
824
825 for_each_cpu(j, policy->cpus) {
826 struct cpufreq_policy *managed_policy;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800827 struct device *cpu_dev;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400828
829 if (j == cpu)
830 continue;
831 if (!cpu_online(j))
832 continue;
833
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200834 pr_debug("CPU %u already managed, adding link\n", j);
Dave Jones19d6f7e2009-07-08 17:35:39 -0400835 managed_policy = cpufreq_cpu_get(cpu);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800836 cpu_dev = get_cpu_device(j);
837 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Dave Jones19d6f7e2009-07-08 17:35:39 -0400838 "cpufreq");
839 if (ret) {
840 cpufreq_cpu_put(managed_policy);
841 return ret;
842 }
843 }
844 return ret;
845}
846
Alex Chiangcf3289d2009-11-17 20:27:08 -0700847static int cpufreq_add_dev_interface(unsigned int cpu,
848 struct cpufreq_policy *policy,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800849 struct device *dev)
Dave Jones909a6942009-07-08 18:05:42 -0400850{
Dave Jonesecf7e462009-07-08 18:48:47 -0400851 struct cpufreq_policy new_policy;
Dave Jones909a6942009-07-08 18:05:42 -0400852 struct freq_attr **drv_attr;
853 unsigned long flags;
854 int ret = 0;
855 unsigned int j;
856
857 /* prepare interface data */
858 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800859 &dev->kobj, "cpufreq");
Dave Jones909a6942009-07-08 18:05:42 -0400860 if (ret)
861 return ret;
862
863 /* set up files for this cpu device */
864 drv_attr = cpufreq_driver->attr;
865 while ((drv_attr) && (*drv_attr)) {
866 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
867 if (ret)
868 goto err_out_kobj_put;
869 drv_attr++;
870 }
871 if (cpufreq_driver->get) {
872 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
873 if (ret)
874 goto err_out_kobj_put;
875 }
876 if (cpufreq_driver->target) {
877 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
878 if (ret)
879 goto err_out_kobj_put;
880 }
Thomas Renningere2f74f32009-11-19 12:31:01 +0100881 if (cpufreq_driver->bios_limit) {
882 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
883 if (ret)
884 goto err_out_kobj_put;
885 }
Dave Jones909a6942009-07-08 18:05:42 -0400886
887 spin_lock_irqsave(&cpufreq_driver_lock, flags);
888 for_each_cpu(j, policy->cpus) {
Julia Lawallbec037a2010-08-05 22:23:00 +0200889 if (!cpu_online(j))
890 continue;
Dave Jones909a6942009-07-08 18:05:42 -0400891 per_cpu(cpufreq_cpu_data, j) = policy;
Tejun Heof1625062009-10-29 22:34:13 +0900892 per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
Dave Jones909a6942009-07-08 18:05:42 -0400893 }
894 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
895
896 ret = cpufreq_add_dev_symlink(cpu, policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400897 if (ret)
898 goto err_out_kobj_put;
899
900 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
901 /* assure that the starting sequence is run in __cpufreq_set_policy */
902 policy->governor = NULL;
903
904 /* set default policy */
905 ret = __cpufreq_set_policy(policy, &new_policy);
906 policy->user_policy.policy = policy->policy;
907 policy->user_policy.governor = policy->governor;
908
909 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200910 pr_debug("setting policy failed\n");
Dave Jonesecf7e462009-07-08 18:48:47 -0400911 if (cpufreq_driver->exit)
912 cpufreq_driver->exit(policy);
913 }
Dave Jones909a6942009-07-08 18:05:42 -0400914 return ret;
915
916err_out_kobj_put:
917 kobject_put(&policy->kobj);
918 wait_for_completion(&policy->kobj_unregister);
919 return ret;
920}
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923/**
924 * cpufreq_add_dev - add a CPU device
925 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500926 * Adds the cpufreq interface for a CPU device.
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400927 *
928 * The Oracle says: try running cpufreq registration/unregistration concurrently
929 * with with cpu hotplugging and all hell will break loose. Tried to clean this
930 * mess up, but more thorough testing is needed. - Mathieu
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800932static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800934 unsigned int cpu = dev->id;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500935 int ret = 0, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 unsigned long flags;
938 unsigned int j;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500939#ifdef CONFIG_HOTPLUG_CPU
940 int sibling;
941#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Ashok Rajc32b6b82005-10-30 14:59:54 -0800943 if (cpu_is_offline(cpu))
944 return 0;
945
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200946 pr_debug("adding CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948#ifdef CONFIG_SMP
949 /* check whether a different CPU already registered this
950 * CPU because it is in the same boat. */
951 policy = cpufreq_cpu_get(cpu);
952 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -0500953 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 return 0;
955 }
956#endif
957
958 if (!try_module_get(cpufreq_driver->owner)) {
959 ret = -EINVAL;
960 goto module_out;
961 }
962
Dave Jones059019a2009-07-08 16:30:03 -0400963 ret = -ENOMEM;
Dave Jonese98df502005-10-20 15:17:43 -0700964 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
Dave Jones059019a2009-07-08 16:30:03 -0400965 if (!policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 goto nomem_out;
Dave Jones059019a2009-07-08 16:30:03 -0400967
968 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400969 goto err_free_policy;
Dave Jones059019a2009-07-08 16:30:03 -0400970
971 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400972 goto err_free_cpumask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 policy->cpu = cpu;
Rusty Russell835481d2009-01-04 05:18:06 -0800975 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800977 /* Initially set CPU itself as the policy_cpu */
Tejun Heof1625062009-10-29 22:34:13 +0900978 per_cpu(cpufreq_policy_cpu, cpu) = cpu;
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400979 ret = (lock_policy_rwsem_write(cpu) < 0);
980 WARN_ON(ret);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 init_completion(&policy->kobj_unregister);
David Howells65f27f32006-11-22 14:55:48 +0000983 INIT_WORK(&policy->update, handle_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Thomas Renninger8122c6c2007-10-02 13:28:09 -0700985 /* Set governor before ->init, so that driver could check it */
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500986#ifdef CONFIG_HOTPLUG_CPU
987 for_each_online_cpu(sibling) {
988 struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
989 if (cp && cp->governor &&
990 (cpumask_test_cpu(cpu, cp->related_cpus))) {
991 policy->governor = cp->governor;
992 found = 1;
993 break;
994 }
995 }
996#endif
997 if (!found)
998 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 /* call driver. From then on the cpufreq must be able
1000 * to accept all calls to ->verify and ->setpolicy for this CPU
1001 */
1002 ret = cpufreq_driver->init(policy);
1003 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001004 pr_debug("initialization failed\n");
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001005 goto err_unlock_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
Mike Chan187d9f42008-12-04 12:19:17 -08001007 policy->user_policy.min = policy->min;
1008 policy->user_policy.max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Thomas Renningera1531ac2008-07-29 22:32:58 -07001010 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1011 CPUFREQ_START, policy);
1012
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001013 ret = cpufreq_add_dev_policy(cpu, policy, dev);
Thomas Renninger4bfa0422009-07-24 15:25:03 +02001014 if (ret) {
1015 if (ret > 0)
1016 /* This is a managed cpu, symlink created,
1017 exit with 0 */
1018 ret = 0;
Dave Jonesecf7e462009-07-08 18:48:47 -04001019 goto err_unlock_policy;
Thomas Renninger4bfa0422009-07-24 15:25:03 +02001020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001022 ret = cpufreq_add_dev_interface(cpu, policy, dev);
Dave Jones19d6f7e2009-07-08 17:35:39 -04001023 if (ret)
1024 goto err_out_unregister;
Dave Jones8ff69732006-03-05 03:37:23 -05001025
Lothar Waßmanndca02612008-05-29 17:54:52 +02001026 unlock_policy_rwsem_write(cpu);
1027
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001028 kobject_uevent(&policy->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 module_put(cpufreq_driver->owner);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001030 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 return 0;
1033
1034
1035err_out_unregister:
1036 spin_lock_irqsave(&cpufreq_driver_lock, flags);
Rusty Russell835481d2009-01-04 05:18:06 -08001037 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -07001038 per_cpu(cpufreq_cpu_data, j) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1040
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -08001041 kobject_put(&policy->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 wait_for_completion(&policy->kobj_unregister);
1043
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001044err_unlock_policy:
Dave Jones45709112008-03-05 14:07:34 -05001045 unlock_policy_rwsem_write(cpu);
Xiaotian Fengcad70a62010-07-20 20:11:02 +08001046 free_cpumask_var(policy->related_cpus);
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001047err_free_cpumask:
1048 free_cpumask_var(policy->cpus);
1049err_free_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 kfree(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051nomem_out:
1052 module_put(cpufreq_driver->owner);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001053module_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return ret;
1055}
1056
1057
1058/**
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001059 * __cpufreq_remove_dev - remove a CPU device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 *
1061 * Removes the cpufreq interface for a CPU device.
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001062 * Caller should already have policy_rwsem in write mode for this CPU.
1063 * This routine frees the rwsem before returning.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001065static int __cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001067 unsigned int cpu = dev->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 unsigned long flags;
1069 struct cpufreq_policy *data;
Amerigo Wang499bca92010-03-04 03:23:46 -05001070 struct kobject *kobj;
1071 struct completion *cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072#ifdef CONFIG_SMP
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001073 struct device *cpu_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 unsigned int j;
1075#endif
1076
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001077 pr_debug("unregistering CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 spin_lock_irqsave(&cpufreq_driver_lock, flags);
Mike Travis7a6aedf2008-03-25 15:06:53 -07001080 data = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 if (!data) {
1083 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001084 unlock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return -EINVAL;
1086 }
Mike Travis7a6aedf2008-03-25 15:06:53 -07001087 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089
1090#ifdef CONFIG_SMP
1091 /* if this isn't the CPU which is the parent of the kobj, we
Dave Jones32ee8c32006-02-28 00:43:23 -05001092 * only need to unlink, put and exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 */
1094 if (unlikely(cpu != data->cpu)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001095 pr_debug("removing link\n");
Rusty Russell835481d2009-01-04 05:18:06 -08001096 cpumask_clear_cpu(cpu, data->cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001098 kobj = &dev->kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 cpufreq_cpu_put(data);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001100 unlock_policy_rwsem_write(cpu);
Amerigo Wang499bca92010-03-04 03:23:46 -05001101 sysfs_remove_link(kobj, "cpufreq");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 return 0;
1103 }
1104#endif
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106#ifdef CONFIG_SMP
Thomas Renninger084f3492007-07-09 11:35:28 -07001107
1108#ifdef CONFIG_HOTPLUG_CPU
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001109 strncpy(per_cpu(cpufreq_policy_save, cpu).gov, data->governor->name,
Dmitry Monakhove77b89f2009-10-05 00:38:55 +04001110 CPUFREQ_NAME_LEN);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001111 per_cpu(cpufreq_policy_save, cpu).min = data->min;
1112 per_cpu(cpufreq_policy_save, cpu).max = data->max;
1113 pr_debug("Saving CPU%d policy min %d and max %d\n",
1114 cpu, data->min, data->max);
Thomas Renninger084f3492007-07-09 11:35:28 -07001115#endif
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 /* if we have other CPUs still registered, we need to unlink them,
1118 * or else wait_for_completion below will lock up. Clean the
Mike Travis7a6aedf2008-03-25 15:06:53 -07001119 * per_cpu(cpufreq_cpu_data) while holding the lock, and remove
1120 * the sysfs links afterwards.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 */
Rusty Russell835481d2009-01-04 05:18:06 -08001122 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1123 for_each_cpu(j, data->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 if (j == cpu)
1125 continue;
Mike Travis7a6aedf2008-03-25 15:06:53 -07001126 per_cpu(cpufreq_cpu_data, j) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
1128 }
1129
1130 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1131
Rusty Russell835481d2009-01-04 05:18:06 -08001132 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1133 for_each_cpu(j, data->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (j == cpu)
1135 continue;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001136 pr_debug("removing link for cpu %u\n", j);
Thomas Renninger084f3492007-07-09 11:35:28 -07001137#ifdef CONFIG_HOTPLUG_CPU
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001138 strncpy(per_cpu(cpufreq_policy_save, j).gov,
Dmitry Monakhove77b89f2009-10-05 00:38:55 +04001139 data->governor->name, CPUFREQ_NAME_LEN);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001140 per_cpu(cpufreq_policy_save, j).min = data->min;
1141 per_cpu(cpufreq_policy_save, j).max = data->max;
1142 pr_debug("Saving CPU%d policy min %d and max %d\n",
1143 j, data->min, data->max);
Thomas Renninger084f3492007-07-09 11:35:28 -07001144#endif
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001145 cpu_dev = get_cpu_device(j);
1146 kobj = &cpu_dev->kobj;
Amerigo Wang499bca92010-03-04 03:23:46 -05001147 unlock_policy_rwsem_write(cpu);
1148 sysfs_remove_link(kobj, "cpufreq");
1149 lock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 cpufreq_cpu_put(data);
1151 }
1152 }
1153#else
1154 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1155#endif
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (cpufreq_driver->target)
1158 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001159
Amerigo Wang499bca92010-03-04 03:23:46 -05001160 kobj = &data->kobj;
1161 cmp = &data->kobj_unregister;
1162 unlock_policy_rwsem_write(cpu);
1163 kobject_put(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 /* we need to make sure that the underlying kobj is actually
Dave Jones32ee8c32006-02-28 00:43:23 -05001166 * not referenced anymore by anybody before we proceed with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 * unloading.
1168 */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001169 pr_debug("waiting for dropping of refcount\n");
Amerigo Wang499bca92010-03-04 03:23:46 -05001170 wait_for_completion(cmp);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001171 pr_debug("wait complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Amerigo Wang499bca92010-03-04 03:23:46 -05001173 lock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 if (cpufreq_driver->exit)
1175 cpufreq_driver->exit(data);
venkatesh.pallipadi@intel.com7d26e2d2009-07-02 17:08:30 -07001176 unlock_policy_rwsem_write(cpu);
1177
Jacob Shin27ecddc2011-04-27 13:32:11 -05001178#ifdef CONFIG_HOTPLUG_CPU
1179 /* when the CPU which is the parent of the kobj is hotplugged
1180 * offline, check for siblings, and create cpufreq sysfs interface
1181 * and symlinks
1182 */
1183 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1184 /* first sibling now owns the new sysfs dir */
1185 cpumask_clear_cpu(cpu, data->cpus);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001186 cpufreq_add_dev(get_cpu_device(cpumask_first(data->cpus)), NULL);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001187
1188 /* finally remove our own symlink */
1189 lock_policy_rwsem_write(cpu);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001190 __cpufreq_remove_dev(dev, sif);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001191 }
1192#endif
1193
Rusty Russell835481d2009-01-04 05:18:06 -08001194 free_cpumask_var(data->related_cpus);
1195 free_cpumask_var(data->cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 kfree(data);
1197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 return 0;
1199}
1200
1201
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001202static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001203{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001204 unsigned int cpu = dev->id;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001205 int retval;
Venki Pallipadiec282972007-03-26 12:03:19 -07001206
1207 if (cpu_is_offline(cpu))
1208 return 0;
1209
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001210 if (unlikely(lock_policy_rwsem_write(cpu)))
1211 BUG();
1212
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001213 retval = __cpufreq_remove_dev(dev, sif);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001214 return retval;
1215}
1216
1217
David Howells65f27f32006-11-22 14:55:48 +00001218static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
David Howells65f27f32006-11-22 14:55:48 +00001220 struct cpufreq_policy *policy =
1221 container_of(work, struct cpufreq_policy, update);
1222 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001223 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 cpufreq_update_policy(cpu);
1225}
1226
1227/**
1228 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
1229 * @cpu: cpu number
1230 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1231 * @new_freq: CPU frequency the CPU actually runs at
1232 *
Dave Jones29464f22009-01-18 01:37:11 -05001233 * We adjust to current frequency first, and need to clean up later.
1234 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301236static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1237 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
1239 struct cpufreq_freqs freqs;
1240
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001241 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1243
1244 freqs.cpu = cpu;
1245 freqs.old = old_freq;
1246 freqs.new = new_freq;
1247 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
1248 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
1249}
1250
1251
Dave Jones32ee8c32006-02-28 00:43:23 -05001252/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301253 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001254 * @cpu: CPU number
1255 *
1256 * This is the last known freq, without actually getting it from the driver.
1257 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1258 */
1259unsigned int cpufreq_quick_get(unsigned int cpu)
1260{
1261 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301262 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001263
1264 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301265 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001266 cpufreq_cpu_put(policy);
1267 }
1268
Dave Jones4d34a672008-02-07 16:33:49 -05001269 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001270}
1271EXPORT_SYMBOL(cpufreq_quick_get);
1272
Jesse Barnes3d737102011-06-28 10:59:12 -07001273/**
1274 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1275 * @cpu: CPU number
1276 *
1277 * Just return the max possible frequency for a given CPU.
1278 */
1279unsigned int cpufreq_quick_get_max(unsigned int cpu)
1280{
1281 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1282 unsigned int ret_freq = 0;
1283
1284 if (policy) {
1285 ret_freq = policy->max;
1286 cpufreq_cpu_put(policy);
1287 }
1288
1289 return ret_freq;
1290}
1291EXPORT_SYMBOL(cpufreq_quick_get_max);
1292
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001293
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001294static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001296 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301297 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001300 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301302 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301304 if (ret_freq && policy->cur &&
1305 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1306 /* verify no discrepancy between actual and
1307 saved value exists */
1308 if (unlikely(ret_freq != policy->cur)) {
1309 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 schedule_work(&policy->update);
1311 }
1312 }
1313
Dave Jones4d34a672008-02-07 16:33:49 -05001314 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001315}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001317/**
1318 * cpufreq_get - get the current CPU frequency (in kHz)
1319 * @cpu: CPU number
1320 *
1321 * Get the CPU current (static) CPU frequency
1322 */
1323unsigned int cpufreq_get(unsigned int cpu)
1324{
1325 unsigned int ret_freq = 0;
1326 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1327
1328 if (!policy)
1329 goto out;
1330
1331 if (unlikely(lock_policy_rwsem_read(cpu)))
1332 goto out_policy;
1333
1334 ret_freq = __cpufreq_get(cpu);
1335
1336 unlock_policy_rwsem_read(cpu);
1337
1338out_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 cpufreq_cpu_put(policy);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001340out:
Dave Jones4d34a672008-02-07 16:33:49 -05001341 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342}
1343EXPORT_SYMBOL(cpufreq_get);
1344
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001345static struct subsys_interface cpufreq_interface = {
1346 .name = "cpufreq",
1347 .subsys = &cpu_subsys,
1348 .add_dev = cpufreq_add_dev,
1349 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001350};
1351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001354 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1355 *
1356 * This function is only executed for the boot processor. The other CPUs
1357 * have been put offline by means of CPU hotplug.
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001358 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001359static int cpufreq_bp_suspend(void)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001360{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301361 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001362
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001363 int cpu = smp_processor_id();
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001364 struct cpufreq_policy *cpu_policy;
1365
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001366 pr_debug("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001367
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001368 /* If there's no policy for the boot CPU, we have nothing to do. */
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001369 cpu_policy = cpufreq_cpu_get(cpu);
1370 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001371 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001372
1373 if (cpufreq_driver->suspend) {
Rafael J. Wysocki7ca64e22011-03-10 21:13:05 +01001374 ret = cpufreq_driver->suspend(cpu_policy);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001375 if (ret)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001376 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1377 "step on CPU %u\n", cpu_policy->cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001378 }
1379
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001380 cpufreq_cpu_put(cpu_policy);
Dave Jonesc9060492008-02-07 16:32:18 -05001381 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001382}
1383
1384/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001385 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 *
1387 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001388 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1389 * restored. It will verify that the current freq is in sync with
1390 * what we believe it to be. This is a bit later than when it
1391 * should be, but nonethteless it's better than calling
1392 * cpufreq_driver->get() here which might re-enable interrupts...
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001393 *
1394 * This function is only executed for the boot CPU. The other CPUs have not
1395 * been turned on yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001397static void cpufreq_bp_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301399 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001400
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001401 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 struct cpufreq_policy *cpu_policy;
1403
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001404 pr_debug("resuming cpu %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001406 /* If there's no policy for the boot CPU, we have nothing to do. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 cpu_policy = cpufreq_cpu_get(cpu);
1408 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001409 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 if (cpufreq_driver->resume) {
1412 ret = cpufreq_driver->resume(cpu_policy);
1413 if (ret) {
1414 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1415 "step on CPU %u\n", cpu_policy->cpu);
Dave Jonesc9060492008-02-07 16:32:18 -05001416 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 }
1418 }
1419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 schedule_work(&cpu_policy->update);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001421
Dave Jonesc9060492008-02-07 16:32:18 -05001422fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424}
1425
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001426static struct syscore_ops cpufreq_syscore_ops = {
1427 .suspend = cpufreq_bp_suspend,
1428 .resume = cpufreq_bp_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429};
1430
1431
1432/*********************************************************************
1433 * NOTIFIER LISTS INTERFACE *
1434 *********************************************************************/
1435
1436/**
1437 * cpufreq_register_notifier - register a driver with cpufreq
1438 * @nb: notifier function to register
1439 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1440 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001441 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 * are notified about clock rate changes (once before and once after
1443 * the transition), or a list of drivers that are notified about
1444 * changes in cpufreq policy.
1445 *
1446 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001447 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 */
1449int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1450{
1451 int ret;
1452
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001453 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 switch (list) {
1456 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001457 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001458 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 break;
1460 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001461 ret = blocking_notifier_chain_register(
1462 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 break;
1464 default:
1465 ret = -EINVAL;
1466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 return ret;
1469}
1470EXPORT_SYMBOL(cpufreq_register_notifier);
1471
1472
1473/**
1474 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1475 * @nb: notifier block to be unregistered
1476 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1477 *
1478 * Remove a driver from the CPU frequency notifier list.
1479 *
1480 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001481 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 */
1483int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1484{
1485 int ret;
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 switch (list) {
1488 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001489 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001490 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 break;
1492 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001493 ret = blocking_notifier_chain_unregister(
1494 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 break;
1496 default:
1497 ret = -EINVAL;
1498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500 return ret;
1501}
1502EXPORT_SYMBOL(cpufreq_unregister_notifier);
1503
1504
1505/*********************************************************************
1506 * GOVERNORS *
1507 *********************************************************************/
1508
1509
1510int __cpufreq_driver_target(struct cpufreq_policy *policy,
1511 unsigned int target_freq,
1512 unsigned int relation)
1513{
1514 int retval = -EINVAL;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001515
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001516 if (cpufreq_disabled())
1517 return -ENODEV;
1518
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001519 pr_debug("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 target_freq, relation);
1521 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1522 retval = cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 return retval;
1525}
1526EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528int cpufreq_driver_target(struct cpufreq_policy *policy,
1529 unsigned int target_freq,
1530 unsigned int relation)
1531{
Julia Lawallf1829e42008-07-25 22:44:53 +02001532 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
1534 policy = cpufreq_cpu_get(policy->cpu);
1535 if (!policy)
Julia Lawallf1829e42008-07-25 22:44:53 +02001536 goto no_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001538 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
Julia Lawallf1829e42008-07-25 22:44:53 +02001539 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 ret = __cpufreq_driver_target(policy, target_freq, relation);
1542
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001543 unlock_policy_rwsem_write(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Julia Lawallf1829e42008-07-25 22:44:53 +02001545fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 cpufreq_cpu_put(policy);
Julia Lawallf1829e42008-07-25 22:44:53 +02001547no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 return ret;
1549}
1550EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1551
venkatesh.pallipadi@intel.combf0b90e2008-08-04 11:59:07 -07001552int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu)
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001553{
1554 int ret = 0;
1555
1556 policy = cpufreq_cpu_get(policy->cpu);
1557 if (!policy)
1558 return -EINVAL;
1559
venkatesh.pallipadi@intel.combf0b90e2008-08-04 11:59:07 -07001560 if (cpu_online(cpu) && cpufreq_driver->getavg)
1561 ret = cpufreq_driver->getavg(policy, cpu);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001562
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001563 cpufreq_cpu_put(policy);
1564 return ret;
1565}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001566EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001567
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001568/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001569 * when "event" is CPUFREQ_GOV_LIMITS
1570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301572static int __cpufreq_governor(struct cpufreq_policy *policy,
1573 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
Dave Jonescc993ca2005-07-28 09:43:56 -07001575 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001576
1577 /* Only must be defined when default governor is known to have latency
1578 restrictions, like e.g. conservative or ondemand.
1579 That this is the case is already ensured in Kconfig
1580 */
1581#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1582 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1583#else
1584 struct cpufreq_governor *gov = NULL;
1585#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001586
1587 if (policy->governor->max_transition_latency &&
1588 policy->cpuinfo.transition_latency >
1589 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07001590 if (!gov)
1591 return -EINVAL;
1592 else {
1593 printk(KERN_WARNING "%s governor failed, too long"
1594 " transition latency of HW, fallback"
1595 " to %s governor\n",
1596 policy->governor->name,
1597 gov->name);
1598 policy->governor = gov;
1599 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
1602 if (!try_module_get(policy->governor->owner))
1603 return -EINVAL;
1604
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001605 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301606 policy->cpu, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 ret = policy->governor->governor(policy, event);
1608
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301609 /* we keep one module reference alive for
1610 each CPU governed by this CPU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 if ((event != CPUFREQ_GOV_START) || ret)
1612 module_put(policy->governor->owner);
1613 if ((event == CPUFREQ_GOV_STOP) && !ret)
1614 module_put(policy->governor->owner);
1615
1616 return ret;
1617}
1618
1619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620int cpufreq_register_governor(struct cpufreq_governor *governor)
1621{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001622 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 if (!governor)
1625 return -EINVAL;
1626
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001627 if (cpufreq_disabled())
1628 return -ENODEV;
1629
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001630 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001631
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001632 err = -EBUSY;
1633 if (__find_governor(governor->name) == NULL) {
1634 err = 0;
1635 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Dave Jones32ee8c32006-02-28 00:43:23 -05001638 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001639 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640}
1641EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1642
1643
1644void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1645{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001646#ifdef CONFIG_HOTPLUG_CPU
1647 int cpu;
1648#endif
1649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 if (!governor)
1651 return;
1652
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001653 if (cpufreq_disabled())
1654 return;
1655
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001656#ifdef CONFIG_HOTPLUG_CPU
1657 for_each_present_cpu(cpu) {
1658 if (cpu_online(cpu))
1659 continue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001660 if (!strcmp(per_cpu(cpufreq_policy_save, cpu).gov,
1661 governor->name))
1662 strcpy(per_cpu(cpufreq_policy_save, cpu).gov, "\0");
1663 per_cpu(cpufreq_policy_save, cpu).min = 0;
1664 per_cpu(cpufreq_policy_save, cpu).max = 0;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001665 }
1666#endif
1667
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001668 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001670 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 return;
1672}
1673EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1674
1675
1676
1677/*********************************************************************
1678 * POLICY INTERFACE *
1679 *********************************************************************/
1680
1681/**
1682 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05001683 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1684 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 *
1686 * Reads the current cpufreq policy.
1687 */
1688int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1689{
1690 struct cpufreq_policy *cpu_policy;
1691 if (!policy)
1692 return -EINVAL;
1693
1694 cpu_policy = cpufreq_cpu_get(cpu);
1695 if (!cpu_policy)
1696 return -EINVAL;
1697
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
1700 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 return 0;
1702}
1703EXPORT_SYMBOL(cpufreq_get_policy);
1704
1705
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001706/*
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301707 * data : current policy.
1708 * policy : policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001709 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301710static int __cpufreq_set_policy(struct cpufreq_policy *data,
1711 struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
1713 int ret = 0;
1714
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001715 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 policy->min, policy->max);
1717
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301718 memcpy(&policy->cpuinfo, &data->cpuinfo,
1719 sizeof(struct cpufreq_cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Yi Yang53391fa2008-01-30 13:33:34 +01001721 if (policy->min > data->max || policy->max < data->min) {
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02001722 ret = -EINVAL;
1723 goto error_out;
1724 }
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 /* verify the cpu speed can be set within this limit */
1727 ret = cpufreq_driver->verify(policy);
1728 if (ret)
1729 goto error_out;
1730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001732 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1733 CPUFREQ_ADJUST, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
1735 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001736 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1737 CPUFREQ_INCOMPATIBLE, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 /* verify the cpu speed can be set within this limit,
1740 which might be different to the first one */
1741 ret = cpufreq_driver->verify(policy);
Alan Sterne041c682006-03-27 01:16:30 -08001742 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001746 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1747 CPUFREQ_NOTIFY, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Dave Jones7d5e3502006-02-02 17:03:42 -05001749 data->min = policy->min;
1750 data->max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001752 pr_debug("new min and max freqs are %u - %u kHz\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301753 data->min, data->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
1755 if (cpufreq_driver->setpolicy) {
1756 data->policy = policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001757 pr_debug("setting range\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 ret = cpufreq_driver->setpolicy(policy);
1759 } else {
1760 if (policy->governor != data->governor) {
1761 /* save old, working values */
1762 struct cpufreq_governor *old_gov = data->governor;
1763
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001764 pr_debug("governor switch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 /* end old governor */
Andrej Gelenbergffe62752010-05-14 15:15:58 -07001767 if (data->governor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1769
1770 /* start new governor */
1771 data->governor = policy->governor;
1772 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1773 /* new governor failed, so re-start old one */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001774 pr_debug("starting governor %s failed\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301775 data->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 if (old_gov) {
1777 data->governor = old_gov;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301778 __cpufreq_governor(data,
1779 CPUFREQ_GOV_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 }
1781 ret = -EINVAL;
1782 goto error_out;
1783 }
1784 /* might be a policy change, too, so fall through */
1785 }
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001786 pr_debug("governor: change or update limits\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1788 }
1789
Dave Jones7d5e3502006-02-02 17:03:42 -05001790error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 return ret;
1792}
1793
1794/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1796 * @cpu: CPU which shall be re-evaluated
1797 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001798 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 * at different times.
1800 */
1801int cpufreq_update_policy(unsigned int cpu)
1802{
1803 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1804 struct cpufreq_policy policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02001805 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Julia Lawallf1829e42008-07-25 22:44:53 +02001807 if (!data) {
1808 ret = -ENODEV;
1809 goto no_policy;
1810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Julia Lawallf1829e42008-07-25 22:44:53 +02001812 if (unlikely(lock_policy_rwsem_write(cpu))) {
1813 ret = -EINVAL;
1814 goto fail;
1815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001817 pr_debug("updating policy for CPU %u\n", cpu);
Dave Jones7d5e3502006-02-02 17:03:42 -05001818 memcpy(&policy, data, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 policy.min = data->user_policy.min;
1820 policy.max = data->user_policy.max;
1821 policy.policy = data->user_policy.policy;
1822 policy.governor = data->user_policy.governor;
1823
Thomas Renninger0961dd02006-01-26 18:46:33 +01001824 /* BIOS might change freq behind our back
1825 -> ask driver for current freq and notify governors about a change */
1826 if (cpufreq_driver->get) {
1827 policy.cur = cpufreq_driver->get(cpu);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001828 if (!data->cur) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001829 pr_debug("Driver did not initialize current freq");
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001830 data->cur = policy.cur;
1831 } else {
1832 if (data->cur != policy.cur)
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301833 cpufreq_out_of_sync(cpu, data->cur,
1834 policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001835 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01001836 }
1837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 ret = __cpufreq_set_policy(data, &policy);
1839
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001840 unlock_policy_rwsem_write(cpu);
1841
Julia Lawallf1829e42008-07-25 22:44:53 +02001842fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 cpufreq_cpu_put(data);
Julia Lawallf1829e42008-07-25 22:44:53 +02001844no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 return ret;
1846}
1847EXPORT_SYMBOL(cpufreq_update_policy);
1848
Satyam Sharmadd184a02007-10-02 13:28:14 -07001849static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08001850 unsigned long action, void *hcpu)
1851{
1852 unsigned int cpu = (unsigned long)hcpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001853 struct device *dev;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001854
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001855 dev = get_cpu_device(cpu);
1856 if (dev) {
Ashok Rajc32b6b82005-10-30 14:59:54 -08001857 switch (action) {
1858 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001859 case CPU_ONLINE_FROZEN:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001860 cpufreq_add_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001861 break;
1862 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001863 case CPU_DOWN_PREPARE_FROZEN:
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001864 if (unlikely(lock_policy_rwsem_write(cpu)))
1865 BUG();
1866
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001867 __cpufreq_remove_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001868 break;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001869 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001870 case CPU_DOWN_FAILED_FROZEN:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001871 cpufreq_add_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001872 break;
1873 }
1874 }
1875 return NOTIFY_OK;
1876}
1877
Neal Buckendahl9c36f742010-06-22 22:02:44 -05001878static struct notifier_block __refdata cpufreq_cpu_notifier = {
Ashok Rajc32b6b82005-10-30 14:59:54 -08001879 .notifier_call = cpufreq_cpu_callback,
1880};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
1882/*********************************************************************
1883 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1884 *********************************************************************/
1885
1886/**
1887 * cpufreq_register_driver - register a CPU Frequency driver
1888 * @driver_data: A struct cpufreq_driver containing the values#
1889 * submitted by the CPU Frequency driver.
1890 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001891 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05001893 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 *
1895 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001896int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
1898 unsigned long flags;
1899 int ret;
1900
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001901 if (cpufreq_disabled())
1902 return -ENODEV;
1903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 if (!driver_data || !driver_data->verify || !driver_data->init ||
1905 ((!driver_data->setpolicy) && (!driver_data->target)))
1906 return -EINVAL;
1907
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001908 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
1910 if (driver_data->setpolicy)
1911 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1912
1913 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1914 if (cpufreq_driver) {
1915 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1916 return -EBUSY;
1917 }
1918 cpufreq_driver = driver_data;
1919 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1920
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001921 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001922 if (ret)
1923 goto err_null_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001925 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 int i;
1927 ret = -ENODEV;
1928
1929 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07001930 for (i = 0; i < nr_cpu_ids; i++)
1931 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07001933 break;
1934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
1936 /* if all ->init() calls failed, unregister */
1937 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001938 pr_debug("no CPU initialized for driver %s\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301939 driver_data->name);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001940 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 }
1942 }
1943
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001944 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001945 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001947 return 0;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001948err_if_unreg:
1949 subsys_interface_unregister(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001950err_null_driver:
1951 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1952 cpufreq_driver = NULL;
1953 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05001954 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955}
1956EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1957
1958
1959/**
1960 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1961 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001962 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 * the right to do so, i.e. if you have succeeded in initialising before!
1964 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1965 * currently not initialised.
1966 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001967int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
1969 unsigned long flags;
1970
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001971 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001974 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001976 subsys_interface_unregister(&cpufreq_interface);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001977 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1980 cpufreq_driver = NULL;
1981 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1982
1983 return 0;
1984}
1985EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001986
1987static int __init cpufreq_core_init(void)
1988{
1989 int cpu;
1990
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001991 if (cpufreq_disabled())
1992 return -ENODEV;
1993
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001994 for_each_possible_cpu(cpu) {
Tejun Heof1625062009-10-29 22:34:13 +09001995 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001996 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
1997 }
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02001998
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001999 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002000 BUG_ON(!cpufreq_global_kobject);
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01002001 register_syscore_ops(&cpufreq_syscore_ops);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002002
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002003 return 0;
2004}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002005core_initcall(cpufreq_core_init);