blob: 36375c05276d2df71ebadc2af8504bfba2072656 [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
133static LIST_HEAD(cpufreq_governor_list);
Dave Jones29464f22009-01-18 01:37:11 -0500134static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Dave Jones7d5e3502006-02-02 17:03:42 -0500136struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 struct cpufreq_policy *data;
139 unsigned long flags;
140
Mike Travis7a6aedf2008-03-25 15:06:53 -0700141 if (cpu >= nr_cpu_ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 goto err_out;
143
144 /* get the cpufreq driver */
145 spin_lock_irqsave(&cpufreq_driver_lock, flags);
146
147 if (!cpufreq_driver)
148 goto err_out_unlock;
149
150 if (!try_module_get(cpufreq_driver->owner))
151 goto err_out_unlock;
152
153
154 /* get the CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -0700155 data = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 if (!data)
158 goto err_out_put_module;
159
160 if (!kobject_get(&data->kobj))
161 goto err_out_put_module;
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return data;
165
Dave Jones7d5e3502006-02-02 17:03:42 -0500166err_out_put_module:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 module_put(cpufreq_driver->owner);
Dave Jones7d5e3502006-02-02 17:03:42 -0500168err_out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones7d5e3502006-02-02 17:03:42 -0500170err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return NULL;
172}
173EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
174
Dave Jones7d5e3502006-02-02 17:03:42 -0500175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176void cpufreq_cpu_put(struct cpufreq_policy *data)
177{
178 kobject_put(&data->kobj);
179 module_put(cpufreq_driver->owner);
180}
181EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
182
183
184/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
186 *********************************************************************/
187
188/**
189 * adjust_jiffies - adjust the system "loops_per_jiffy"
190 *
191 * This function alters the system "loops_per_jiffy" for the clock
192 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500193 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 * per-CPU loops_per_jiffy value wherever possible.
195 */
196#ifndef CONFIG_SMP
197static unsigned long l_p_j_ref;
198static unsigned int l_p_j_ref_freq;
199
Arjan van de Ven858119e2006-01-14 13:20:43 -0800200static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 if (ci->flags & CPUFREQ_CONST_LOOPS)
203 return;
204
205 if (!l_p_j_ref_freq) {
206 l_p_j_ref = loops_per_jiffy;
207 l_p_j_ref_freq = ci->old;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200208 pr_debug("saving %lu as reference value for loops_per_jiffy; "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530209 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
211 if ((val == CPUFREQ_PRECHANGE && ci->old < ci->new) ||
212 (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) ||
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700213 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530214 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
215 ci->new);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200216 pr_debug("scaling loops_per_jiffy to %lu "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530217 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
219}
220#else
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530221static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
222{
223 return;
224}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225#endif
226
227
228/**
Dave Jonese4472cb2006-01-31 15:53:55 -0800229 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
230 * on frequency transition.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 *
Dave Jonese4472cb2006-01-31 15:53:55 -0800232 * This function calls the transition notifiers and the "adjust_jiffies"
233 * function. It is called twice on all CPU frequency changes that have
Dave Jones32ee8c32006-02-28 00:43:23 -0500234 * external effects.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 */
236void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
237{
Dave Jonese4472cb2006-01-31 15:53:55 -0800238 struct cpufreq_policy *policy;
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 BUG_ON(irqs_disabled());
241
242 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200243 pr_debug("notification %u of frequency transition to %u kHz\n",
Dave Jonese4472cb2006-01-31 15:53:55 -0800244 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Mike Travis7a6aedf2008-03-25 15:06:53 -0700246 policy = per_cpu(cpufreq_cpu_data, freqs->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500250 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800251 * which is not equal to what the cpufreq core thinks is
252 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 */
254 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800255 if ((policy) && (policy->cpu == freqs->cpu) &&
256 (policy->cur) && (policy->cur != freqs->old)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200257 pr_debug("Warning: CPU frequency is"
Dave Jonese4472cb2006-01-31 15:53:55 -0800258 " %u, cpufreq assumed %u kHz.\n",
259 freqs->old, policy->cur);
260 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
262 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700263 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800264 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
266 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 case CPUFREQ_POSTCHANGE:
269 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200270 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
Thomas Renninger6f4f2722010-04-20 13:17:36 +0200271 (unsigned long)freqs->cpu);
272 trace_power_frequency(POWER_PSTATE, freqs->new, freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100273 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700274 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800275 CPUFREQ_POSTCHANGE, freqs);
Amar Singhal99055d52012-02-23 13:54:46 -0800276 if (likely(policy) && likely(policy->cpu == freqs->cpu)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800277 policy->cur = freqs->new;
Amar Singhal99055d52012-02-23 13:54:46 -0800278 sysfs_notify(&policy->kobj, NULL, "scaling_cur_freq");
279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 break;
281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
284
285
286
287/*********************************************************************
288 * SYSFS INTERFACE *
289 *********************************************************************/
290
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700291static struct cpufreq_governor *__find_governor(const char *str_governor)
292{
293 struct cpufreq_governor *t;
294
295 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
Dave Jones29464f22009-01-18 01:37:11 -0500296 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700297 return t;
298
299 return NULL;
300}
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302/**
303 * cpufreq_parse_governor - parse a governor string
304 */
Dave Jones905d77c2008-03-05 14:28:32 -0500305static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 struct cpufreq_governor **governor)
307{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700308 int err = -EINVAL;
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700311 goto out;
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 if (cpufreq_driver->setpolicy) {
314 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
315 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700316 err = 0;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530317 } else if (!strnicmp(str_governor, "powersave",
318 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700320 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700322 } else if (cpufreq_driver->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700324
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800325 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700326
327 t = __find_governor(str_governor);
328
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700329 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700330 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700331
Kees Cook1a8e1462011-05-04 08:38:56 -0700332 mutex_unlock(&cpufreq_governor_mutex);
333 ret = request_module("cpufreq_%s", str_governor);
334 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700335
Kees Cook1a8e1462011-05-04 08:38:56 -0700336 if (ret == 0)
337 t = __find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700338 }
339
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700340 if (t != NULL) {
341 *governor = t;
342 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700344
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800345 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
Dave Jones29464f22009-01-18 01:37:11 -0500347out:
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700348 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530353 * cpufreq_per_cpu_attr_read() / show_##file_name() -
354 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 *
356 * Write out information from cpufreq_driver->policy[cpu]; object must be
357 * "unsigned int".
358 */
359
Dave Jones32ee8c32006-02-28 00:43:23 -0500360#define show_one(file_name, object) \
361static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500362(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500363{ \
Dave Jones29464f22009-01-18 01:37:11 -0500364 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
367show_one(cpuinfo_min_freq, cpuinfo.min_freq);
368show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100369show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370show_one(scaling_min_freq, min);
371show_one(scaling_max_freq, max);
372show_one(scaling_cur_freq, cur);
373
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530374static int __cpufreq_set_policy(struct cpufreq_policy *data,
375 struct cpufreq_policy *policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377/**
378 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
379 */
380#define store_one(file_name, object) \
381static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500382(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{ \
384 unsigned int ret = -EINVAL; \
385 struct cpufreq_policy new_policy; \
386 \
387 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
388 if (ret) \
389 return -EINVAL; \
390 \
Dave Jones29464f22009-01-18 01:37:11 -0500391 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (ret != 1) \
393 return -EINVAL; \
394 \
Thomas Renninger7970e082006-04-13 15:14:04 +0200395 ret = __cpufreq_set_policy(policy, &new_policy); \
396 policy->user_policy.object = policy->object; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 \
398 return ret ? ret : count; \
399}
400
Dave Jones29464f22009-01-18 01:37:11 -0500401store_one(scaling_min_freq, min);
402store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404/**
405 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
406 */
Dave Jones905d77c2008-03-05 14:28:32 -0500407static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
408 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800410 unsigned int cur_freq = __cpufreq_get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (!cur_freq)
412 return sprintf(buf, "<unknown>");
413 return sprintf(buf, "%u\n", cur_freq);
414}
415
416
417/**
418 * show_scaling_governor - show the current policy for the specified CPU
419 */
Dave Jones905d77c2008-03-05 14:28:32 -0500420static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Dave Jones29464f22009-01-18 01:37:11 -0500422 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return sprintf(buf, "powersave\n");
424 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
425 return sprintf(buf, "performance\n");
426 else if (policy->governor)
Dave Jones29464f22009-01-18 01:37:11 -0500427 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n",
428 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return -EINVAL;
430}
431
432
433/**
434 * store_scaling_governor - store policy for the specified CPU
435 */
Dave Jones905d77c2008-03-05 14:28:32 -0500436static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
437 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
439 unsigned int ret = -EINVAL;
440 char str_governor[16];
441 struct cpufreq_policy new_policy;
442
443 ret = cpufreq_get_policy(&new_policy, policy->cpu);
444 if (ret)
445 return ret;
446
Dave Jones29464f22009-01-18 01:37:11 -0500447 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (ret != 1)
449 return -EINVAL;
450
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530451 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
452 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return -EINVAL;
454
Thomas Renninger7970e082006-04-13 15:14:04 +0200455 /* Do not use cpufreq_set_policy here or the user_policy.max
456 will be wrongly overridden */
Thomas Renninger7970e082006-04-13 15:14:04 +0200457 ret = __cpufreq_set_policy(policy, &new_policy);
458
459 policy->user_policy.policy = policy->policy;
460 policy->user_policy.governor = policy->governor;
Thomas Renninger7970e082006-04-13 15:14:04 +0200461
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530462 if (ret)
463 return ret;
464 else
465 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
468/**
469 * show_scaling_driver - show the cpufreq driver currently loaded
470 */
Dave Jones905d77c2008-03-05 14:28:32 -0500471static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
474}
475
476/**
477 * show_scaling_available_governors - show the available CPUfreq governors
478 */
Dave Jones905d77c2008-03-05 14:28:32 -0500479static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
480 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
482 ssize_t i = 0;
483 struct cpufreq_governor *t;
484
485 if (!cpufreq_driver->target) {
486 i += sprintf(buf, "performance powersave");
487 goto out;
488 }
489
490 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
Dave Jones29464f22009-01-18 01:37:11 -0500491 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
492 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 goto out;
494 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
495 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500496out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 i += sprintf(&buf[i], "\n");
498 return i;
499}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700500
Rusty Russell835481d2009-01-04 05:18:06 -0800501static ssize_t show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 ssize_t i = 0;
504 unsigned int cpu;
505
Rusty Russell835481d2009-01-04 05:18:06 -0800506 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (i)
508 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
509 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
510 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500511 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513 i += sprintf(&buf[i], "\n");
514 return i;
515}
516
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700517/**
518 * show_related_cpus - show the CPUs affected by each transition even if
519 * hw coordination is in use
520 */
521static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
522{
Rusty Russell835481d2009-01-04 05:18:06 -0800523 if (cpumask_empty(policy->related_cpus))
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700524 return show_cpus(policy->cpus, buf);
525 return show_cpus(policy->related_cpus, buf);
526}
527
528/**
529 * show_affected_cpus - show the CPUs affected by each transition
530 */
531static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
532{
533 return show_cpus(policy->cpus, buf);
534}
535
Venki Pallipadi9e769882007-10-26 10:18:21 -0700536static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500537 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700538{
539 unsigned int freq = 0;
540 unsigned int ret;
541
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700542 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700543 return -EINVAL;
544
545 ret = sscanf(buf, "%u", &freq);
546 if (ret != 1)
547 return -EINVAL;
548
549 policy->governor->store_setspeed(policy, freq);
550
551 return count;
552}
553
554static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
555{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700556 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700557 return sprintf(buf, "<unsupported>\n");
558
559 return policy->governor->show_setspeed(policy, buf);
560}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Thomas Renningere2f74f32009-11-19 12:31:01 +0100562/**
563 * show_scaling_driver - show the current cpufreq HW/BIOS limitation
564 */
565static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
566{
567 unsigned int limit;
568 int ret;
569 if (cpufreq_driver->bios_limit) {
570 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
571 if (!ret)
572 return sprintf(buf, "%u\n", limit);
573 }
574 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
575}
576
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200577cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
578cpufreq_freq_attr_ro(cpuinfo_min_freq);
579cpufreq_freq_attr_ro(cpuinfo_max_freq);
580cpufreq_freq_attr_ro(cpuinfo_transition_latency);
581cpufreq_freq_attr_ro(scaling_available_governors);
582cpufreq_freq_attr_ro(scaling_driver);
583cpufreq_freq_attr_ro(scaling_cur_freq);
584cpufreq_freq_attr_ro(bios_limit);
585cpufreq_freq_attr_ro(related_cpus);
586cpufreq_freq_attr_ro(affected_cpus);
587cpufreq_freq_attr_rw(scaling_min_freq);
588cpufreq_freq_attr_rw(scaling_max_freq);
589cpufreq_freq_attr_rw(scaling_governor);
590cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Dave Jones905d77c2008-03-05 14:28:32 -0500592static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 &cpuinfo_min_freq.attr,
594 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100595 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 &scaling_min_freq.attr,
597 &scaling_max_freq.attr,
598 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700599 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 &scaling_governor.attr,
601 &scaling_driver.attr,
602 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700603 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 NULL
605};
606
Thomas Renninger8aa84ad2009-07-24 15:25:05 +0200607struct kobject *cpufreq_global_kobject;
608EXPORT_SYMBOL(cpufreq_global_kobject);
609
Dave Jones29464f22009-01-18 01:37:11 -0500610#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
611#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Dave Jones29464f22009-01-18 01:37:11 -0500613static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Dave Jones905d77c2008-03-05 14:28:32 -0500615 struct cpufreq_policy *policy = to_policy(kobj);
616 struct freq_attr *fattr = to_attr(attr);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500617 ssize_t ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 policy = cpufreq_cpu_get(policy->cpu);
619 if (!policy)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500620 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800621
622 if (lock_policy_rwsem_read(policy->cpu) < 0)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500623 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800624
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530625 if (fattr->show)
626 ret = fattr->show(policy, buf);
627 else
628 ret = -EIO;
629
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800630 unlock_policy_rwsem_read(policy->cpu);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500631fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 cpufreq_cpu_put(policy);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500633no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return ret;
635}
636
Dave Jones905d77c2008-03-05 14:28:32 -0500637static ssize_t store(struct kobject *kobj, struct attribute *attr,
638 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Dave Jones905d77c2008-03-05 14:28:32 -0500640 struct cpufreq_policy *policy = to_policy(kobj);
641 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500642 ssize_t ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 policy = cpufreq_cpu_get(policy->cpu);
644 if (!policy)
Dave Jonesa07530b2008-03-05 14:22:25 -0500645 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800646
647 if (lock_policy_rwsem_write(policy->cpu) < 0)
Dave Jonesa07530b2008-03-05 14:22:25 -0500648 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800649
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530650 if (fattr->store)
651 ret = fattr->store(policy, buf, count);
652 else
653 ret = -EIO;
654
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800655 unlock_policy_rwsem_write(policy->cpu);
Dave Jonesa07530b2008-03-05 14:22:25 -0500656fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 cpufreq_cpu_put(policy);
Dave Jonesa07530b2008-03-05 14:22:25 -0500658no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return ret;
660}
661
Dave Jones905d77c2008-03-05 14:28:32 -0500662static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Dave Jones905d77c2008-03-05 14:28:32 -0500664 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200665 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 complete(&policy->kobj_unregister);
667}
668
Emese Revfy52cf25d2010-01-19 02:58:23 +0100669static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 .show = show,
671 .store = store,
672};
673
674static struct kobj_type ktype_cpufreq = {
675 .sysfs_ops = &sysfs_ops,
676 .default_attrs = default_attrs,
677 .release = cpufreq_sysfs_release,
678};
679
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200680/*
681 * Returns:
682 * Negative: Failure
683 * 0: Success
684 * Positive: When we have a managed CPU and the sysfs got symlinked
685 */
Alex Chiangcf3289d2009-11-17 20:27:08 -0700686static int cpufreq_add_dev_policy(unsigned int cpu,
687 struct cpufreq_policy *policy,
688 struct sys_device *sys_dev)
Dave Jonesecf7e462009-07-08 18:48:47 -0400689{
690 int ret = 0;
691#ifdef CONFIG_SMP
692 unsigned long flags;
693 unsigned int j;
Dave Jonesecf7e462009-07-08 18:48:47 -0400694#ifdef CONFIG_HOTPLUG_CPU
Dmitry Monakhove77b89f2009-10-05 00:38:55 +0400695 struct cpufreq_governor *gov;
696
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700697 gov = __find_governor(per_cpu(cpufreq_policy_save, cpu).gov);
Dmitry Monakhove77b89f2009-10-05 00:38:55 +0400698 if (gov) {
699 policy->governor = gov;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200700 pr_debug("Restoring governor %s for cpu %d\n",
Dave Jonesecf7e462009-07-08 18:48:47 -0400701 policy->governor->name, cpu);
702 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700703 if (per_cpu(cpufreq_policy_save, cpu).min) {
704 policy->min = per_cpu(cpufreq_policy_save, cpu).min;
705 policy->user_policy.min = policy->min;
706 }
707 if (per_cpu(cpufreq_policy_save, cpu).max) {
708 policy->max = per_cpu(cpufreq_policy_save, cpu).max;
709 policy->user_policy.max = policy->max;
710 }
711 pr_debug("Restoring CPU%d min %d and max %d\n",
712 cpu, policy->min, policy->max);
Dave Jonesecf7e462009-07-08 18:48:47 -0400713#endif
714
715 for_each_cpu(j, policy->cpus) {
716 struct cpufreq_policy *managed_policy;
717
718 if (cpu == j)
719 continue;
720
721 /* Check for existing affected CPUs.
722 * They may not be aware of it due to CPU Hotplug.
723 * cpufreq_cpu_put is called when the device is removed
724 * in __cpufreq_remove_dev()
725 */
726 managed_policy = cpufreq_cpu_get(j);
727 if (unlikely(managed_policy)) {
728
729 /* Set proper policy_cpu */
730 unlock_policy_rwsem_write(cpu);
Tejun Heof1625062009-10-29 22:34:13 +0900731 per_cpu(cpufreq_policy_cpu, cpu) = managed_policy->cpu;
Dave Jonesecf7e462009-07-08 18:48:47 -0400732
733 if (lock_policy_rwsem_write(cpu) < 0) {
734 /* Should not go through policy unlock path */
735 if (cpufreq_driver->exit)
736 cpufreq_driver->exit(policy);
737 cpufreq_cpu_put(managed_policy);
738 return -EBUSY;
739 }
740
741 spin_lock_irqsave(&cpufreq_driver_lock, flags);
742 cpumask_copy(managed_policy->cpus, policy->cpus);
743 per_cpu(cpufreq_cpu_data, cpu) = managed_policy;
744 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
745
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200746 pr_debug("CPU already managed, adding link\n");
Dave Jonesecf7e462009-07-08 18:48:47 -0400747 ret = sysfs_create_link(&sys_dev->kobj,
748 &managed_policy->kobj,
749 "cpufreq");
750 if (ret)
751 cpufreq_cpu_put(managed_policy);
752 /*
753 * Success. We only needed to be added to the mask.
754 * Call driver->exit() because only the cpu parent of
755 * the kobj needed to call init().
756 */
757 if (cpufreq_driver->exit)
758 cpufreq_driver->exit(policy);
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200759
760 if (!ret)
761 return 1;
762 else
763 return ret;
Dave Jonesecf7e462009-07-08 18:48:47 -0400764 }
765 }
766#endif
767 return ret;
768}
769
770
Dave Jones19d6f7e2009-07-08 17:35:39 -0400771/* symlink affected CPUs */
Alex Chiangcf3289d2009-11-17 20:27:08 -0700772static int cpufreq_add_dev_symlink(unsigned int cpu,
773 struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400774{
775 unsigned int j;
776 int ret = 0;
777
778 for_each_cpu(j, policy->cpus) {
779 struct cpufreq_policy *managed_policy;
780 struct sys_device *cpu_sys_dev;
781
782 if (j == cpu)
783 continue;
784 if (!cpu_online(j))
785 continue;
786
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200787 pr_debug("CPU %u already managed, adding link\n", j);
Dave Jones19d6f7e2009-07-08 17:35:39 -0400788 managed_policy = cpufreq_cpu_get(cpu);
789 cpu_sys_dev = get_cpu_sysdev(j);
790 ret = sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
791 "cpufreq");
792 if (ret) {
793 cpufreq_cpu_put(managed_policy);
794 return ret;
795 }
796 }
797 return ret;
798}
799
Alex Chiangcf3289d2009-11-17 20:27:08 -0700800static int cpufreq_add_dev_interface(unsigned int cpu,
801 struct cpufreq_policy *policy,
802 struct sys_device *sys_dev)
Dave Jones909a6942009-07-08 18:05:42 -0400803{
Dave Jonesecf7e462009-07-08 18:48:47 -0400804 struct cpufreq_policy new_policy;
Dave Jones909a6942009-07-08 18:05:42 -0400805 struct freq_attr **drv_attr;
806 unsigned long flags;
807 int ret = 0;
808 unsigned int j;
809
810 /* prepare interface data */
811 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
812 &sys_dev->kobj, "cpufreq");
813 if (ret)
814 return ret;
815
816 /* set up files for this cpu device */
817 drv_attr = cpufreq_driver->attr;
818 while ((drv_attr) && (*drv_attr)) {
819 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
820 if (ret)
821 goto err_out_kobj_put;
822 drv_attr++;
823 }
824 if (cpufreq_driver->get) {
825 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
826 if (ret)
827 goto err_out_kobj_put;
828 }
829 if (cpufreq_driver->target) {
830 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
831 if (ret)
832 goto err_out_kobj_put;
833 }
Thomas Renningere2f74f32009-11-19 12:31:01 +0100834 if (cpufreq_driver->bios_limit) {
835 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
836 if (ret)
837 goto err_out_kobj_put;
838 }
Dave Jones909a6942009-07-08 18:05:42 -0400839
840 spin_lock_irqsave(&cpufreq_driver_lock, flags);
841 for_each_cpu(j, policy->cpus) {
Julia Lawallbec037a2010-08-05 22:23:00 +0200842 if (!cpu_online(j))
843 continue;
Dave Jones909a6942009-07-08 18:05:42 -0400844 per_cpu(cpufreq_cpu_data, j) = policy;
Tejun Heof1625062009-10-29 22:34:13 +0900845 per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
Dave Jones909a6942009-07-08 18:05:42 -0400846 }
847 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
848
849 ret = cpufreq_add_dev_symlink(cpu, policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400850 if (ret)
851 goto err_out_kobj_put;
852
853 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
854 /* assure that the starting sequence is run in __cpufreq_set_policy */
855 policy->governor = NULL;
856
857 /* set default policy */
858 ret = __cpufreq_set_policy(policy, &new_policy);
859 policy->user_policy.policy = policy->policy;
860 policy->user_policy.governor = policy->governor;
861
862 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200863 pr_debug("setting policy failed\n");
Dave Jonesecf7e462009-07-08 18:48:47 -0400864 if (cpufreq_driver->exit)
865 cpufreq_driver->exit(policy);
866 }
Dave Jones909a6942009-07-08 18:05:42 -0400867 return ret;
868
869err_out_kobj_put:
870 kobject_put(&policy->kobj);
871 wait_for_completion(&policy->kobj_unregister);
872 return ret;
873}
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876/**
877 * cpufreq_add_dev - add a CPU device
878 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500879 * Adds the cpufreq interface for a CPU device.
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400880 *
881 * The Oracle says: try running cpufreq registration/unregistration concurrently
882 * with with cpu hotplugging and all hell will break loose. Tried to clean this
883 * mess up, but more thorough testing is needed. - Mathieu
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 */
Dave Jones905d77c2008-03-05 14:28:32 -0500885static int cpufreq_add_dev(struct sys_device *sys_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
887 unsigned int cpu = sys_dev->id;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500888 int ret = 0, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 unsigned long flags;
891 unsigned int j;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500892#ifdef CONFIG_HOTPLUG_CPU
893 int sibling;
894#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Ashok Rajc32b6b82005-10-30 14:59:54 -0800896 if (cpu_is_offline(cpu))
897 return 0;
898
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200899 pr_debug("adding CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901#ifdef CONFIG_SMP
902 /* check whether a different CPU already registered this
903 * CPU because it is in the same boat. */
904 policy = cpufreq_cpu_get(cpu);
905 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -0500906 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return 0;
908 }
909#endif
910
911 if (!try_module_get(cpufreq_driver->owner)) {
912 ret = -EINVAL;
913 goto module_out;
914 }
915
Dave Jones059019a2009-07-08 16:30:03 -0400916 ret = -ENOMEM;
Dave Jonese98df502005-10-20 15:17:43 -0700917 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
Dave Jones059019a2009-07-08 16:30:03 -0400918 if (!policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 goto nomem_out;
Dave Jones059019a2009-07-08 16:30:03 -0400920
921 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400922 goto err_free_policy;
Dave Jones059019a2009-07-08 16:30:03 -0400923
924 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400925 goto err_free_cpumask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 policy->cpu = cpu;
Rusty Russell835481d2009-01-04 05:18:06 -0800928 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800930 /* Initially set CPU itself as the policy_cpu */
Tejun Heof1625062009-10-29 22:34:13 +0900931 per_cpu(cpufreq_policy_cpu, cpu) = cpu;
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400932 ret = (lock_policy_rwsem_write(cpu) < 0);
933 WARN_ON(ret);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 init_completion(&policy->kobj_unregister);
David Howells65f27f32006-11-22 14:55:48 +0000936 INIT_WORK(&policy->update, handle_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Thomas Renninger8122c6c2007-10-02 13:28:09 -0700938 /* Set governor before ->init, so that driver could check it */
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500939#ifdef CONFIG_HOTPLUG_CPU
940 for_each_online_cpu(sibling) {
941 struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
942 if (cp && cp->governor &&
943 (cpumask_test_cpu(cpu, cp->related_cpus))) {
944 policy->governor = cp->governor;
945 found = 1;
946 break;
947 }
948 }
949#endif
950 if (!found)
951 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 /* call driver. From then on the cpufreq must be able
953 * to accept all calls to ->verify and ->setpolicy for this CPU
954 */
955 ret = cpufreq_driver->init(policy);
956 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200957 pr_debug("initialization failed\n");
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400958 goto err_unlock_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 }
Mike Chan187d9f42008-12-04 12:19:17 -0800960 policy->user_policy.min = policy->min;
961 policy->user_policy.max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Thomas Renningera1531ac2008-07-29 22:32:58 -0700963 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
964 CPUFREQ_START, policy);
965
Dave Jonesecf7e462009-07-08 18:48:47 -0400966 ret = cpufreq_add_dev_policy(cpu, policy, sys_dev);
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200967 if (ret) {
968 if (ret > 0)
969 /* This is a managed cpu, symlink created,
970 exit with 0 */
971 ret = 0;
Dave Jonesecf7e462009-07-08 18:48:47 -0400972 goto err_unlock_policy;
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Dave Jones909a6942009-07-08 18:05:42 -0400975 ret = cpufreq_add_dev_interface(cpu, policy, sys_dev);
Dave Jones19d6f7e2009-07-08 17:35:39 -0400976 if (ret)
977 goto err_out_unregister;
Dave Jones8ff69732006-03-05 03:37:23 -0500978
Lothar Waßmanndca02612008-05-29 17:54:52 +0200979 unlock_policy_rwsem_write(cpu);
980
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -0400981 kobject_uevent(&policy->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 module_put(cpufreq_driver->owner);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200983 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -0500984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return 0;
986
987
988err_out_unregister:
989 spin_lock_irqsave(&cpufreq_driver_lock, flags);
Rusty Russell835481d2009-01-04 05:18:06 -0800990 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -0700991 per_cpu(cpufreq_cpu_data, j) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
993
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800994 kobject_put(&policy->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 wait_for_completion(&policy->kobj_unregister);
996
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400997err_unlock_policy:
Dave Jones45709112008-03-05 14:07:34 -0500998 unlock_policy_rwsem_write(cpu);
Xiaotian Fengcad70a62010-07-20 20:11:02 +0800999 free_cpumask_var(policy->related_cpus);
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001000err_free_cpumask:
1001 free_cpumask_var(policy->cpus);
1002err_free_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 kfree(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004nomem_out:
1005 module_put(cpufreq_driver->owner);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001006module_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 return ret;
1008}
1009
1010
1011/**
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001012 * __cpufreq_remove_dev - remove a CPU device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 *
1014 * Removes the cpufreq interface for a CPU device.
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001015 * Caller should already have policy_rwsem in write mode for this CPU.
1016 * This routine frees the rwsem before returning.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 */
Dave Jones905d77c2008-03-05 14:28:32 -05001018static int __cpufreq_remove_dev(struct sys_device *sys_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
1020 unsigned int cpu = sys_dev->id;
1021 unsigned long flags;
1022 struct cpufreq_policy *data;
Amerigo Wang499bca92010-03-04 03:23:46 -05001023 struct kobject *kobj;
1024 struct completion *cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025#ifdef CONFIG_SMP
Grant Coadye738cf62005-11-21 21:32:28 -08001026 struct sys_device *cpu_sys_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 unsigned int j;
1028#endif
1029
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001030 pr_debug("unregistering CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 spin_lock_irqsave(&cpufreq_driver_lock, flags);
Mike Travis7a6aedf2008-03-25 15:06:53 -07001033 data = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 if (!data) {
1036 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001037 unlock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return -EINVAL;
1039 }
Mike Travis7a6aedf2008-03-25 15:06:53 -07001040 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042
1043#ifdef CONFIG_SMP
1044 /* if this isn't the CPU which is the parent of the kobj, we
Dave Jones32ee8c32006-02-28 00:43:23 -05001045 * only need to unlink, put and exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 */
1047 if (unlikely(cpu != data->cpu)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001048 pr_debug("removing link\n");
Rusty Russell835481d2009-01-04 05:18:06 -08001049 cpumask_clear_cpu(cpu, data->cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Amerigo Wang499bca92010-03-04 03:23:46 -05001051 kobj = &sys_dev->kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 cpufreq_cpu_put(data);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001053 unlock_policy_rwsem_write(cpu);
Amerigo Wang499bca92010-03-04 03:23:46 -05001054 sysfs_remove_link(kobj, "cpufreq");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 return 0;
1056 }
1057#endif
1058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059#ifdef CONFIG_SMP
Thomas Renninger084f3492007-07-09 11:35:28 -07001060
1061#ifdef CONFIG_HOTPLUG_CPU
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001062 strncpy(per_cpu(cpufreq_policy_save, cpu).gov, data->governor->name,
Dmitry Monakhove77b89f2009-10-05 00:38:55 +04001063 CPUFREQ_NAME_LEN);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001064 per_cpu(cpufreq_policy_save, cpu).min = data->min;
1065 per_cpu(cpufreq_policy_save, cpu).max = data->max;
1066 pr_debug("Saving CPU%d policy min %d and max %d\n",
1067 cpu, data->min, data->max);
Thomas Renninger084f3492007-07-09 11:35:28 -07001068#endif
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 /* if we have other CPUs still registered, we need to unlink them,
1071 * or else wait_for_completion below will lock up. Clean the
Mike Travis7a6aedf2008-03-25 15:06:53 -07001072 * per_cpu(cpufreq_cpu_data) while holding the lock, and remove
1073 * the sysfs links afterwards.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 */
Rusty Russell835481d2009-01-04 05:18:06 -08001075 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1076 for_each_cpu(j, data->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (j == cpu)
1078 continue;
Mike Travis7a6aedf2008-03-25 15:06:53 -07001079 per_cpu(cpufreq_cpu_data, j) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
1081 }
1082
1083 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1084
Rusty Russell835481d2009-01-04 05:18:06 -08001085 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1086 for_each_cpu(j, data->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 if (j == cpu)
1088 continue;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001089 pr_debug("removing link for cpu %u\n", j);
Thomas Renninger084f3492007-07-09 11:35:28 -07001090#ifdef CONFIG_HOTPLUG_CPU
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001091 strncpy(per_cpu(cpufreq_policy_save, j).gov,
Dmitry Monakhove77b89f2009-10-05 00:38:55 +04001092 data->governor->name, CPUFREQ_NAME_LEN);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001093 per_cpu(cpufreq_policy_save, j).min = data->min;
1094 per_cpu(cpufreq_policy_save, j).max = data->max;
1095 pr_debug("Saving CPU%d policy min %d and max %d\n",
1096 j, data->min, data->max);
Thomas Renninger084f3492007-07-09 11:35:28 -07001097#endif
Ashok Rajd434fca2005-10-30 14:59:52 -08001098 cpu_sys_dev = get_cpu_sysdev(j);
Amerigo Wang499bca92010-03-04 03:23:46 -05001099 kobj = &cpu_sys_dev->kobj;
1100 unlock_policy_rwsem_write(cpu);
1101 sysfs_remove_link(kobj, "cpufreq");
1102 lock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 cpufreq_cpu_put(data);
1104 }
1105 }
1106#else
1107 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1108#endif
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (cpufreq_driver->target)
1111 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001112
Amerigo Wang499bca92010-03-04 03:23:46 -05001113 kobj = &data->kobj;
1114 cmp = &data->kobj_unregister;
1115 unlock_policy_rwsem_write(cpu);
1116 kobject_put(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 /* we need to make sure that the underlying kobj is actually
Dave Jones32ee8c32006-02-28 00:43:23 -05001119 * not referenced anymore by anybody before we proceed with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 * unloading.
1121 */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001122 pr_debug("waiting for dropping of refcount\n");
Amerigo Wang499bca92010-03-04 03:23:46 -05001123 wait_for_completion(cmp);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001124 pr_debug("wait complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Amerigo Wang499bca92010-03-04 03:23:46 -05001126 lock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (cpufreq_driver->exit)
1128 cpufreq_driver->exit(data);
venkatesh.pallipadi@intel.com7d26e2d2009-07-02 17:08:30 -07001129 unlock_policy_rwsem_write(cpu);
1130
Jacob Shin27ecddc2011-04-27 13:32:11 -05001131#ifdef CONFIG_HOTPLUG_CPU
1132 /* when the CPU which is the parent of the kobj is hotplugged
1133 * offline, check for siblings, and create cpufreq sysfs interface
1134 * and symlinks
1135 */
1136 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1137 /* first sibling now owns the new sysfs dir */
1138 cpumask_clear_cpu(cpu, data->cpus);
1139 cpufreq_add_dev(get_cpu_sysdev(cpumask_first(data->cpus)));
1140
1141 /* finally remove our own symlink */
1142 lock_policy_rwsem_write(cpu);
1143 __cpufreq_remove_dev(sys_dev);
1144 }
1145#endif
1146
Rusty Russell835481d2009-01-04 05:18:06 -08001147 free_cpumask_var(data->related_cpus);
1148 free_cpumask_var(data->cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 kfree(data);
1150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 return 0;
1152}
1153
1154
Dave Jones905d77c2008-03-05 14:28:32 -05001155static int cpufreq_remove_dev(struct sys_device *sys_dev)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001156{
1157 unsigned int cpu = sys_dev->id;
1158 int retval;
Venki Pallipadiec282972007-03-26 12:03:19 -07001159
1160 if (cpu_is_offline(cpu))
1161 return 0;
1162
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001163 if (unlikely(lock_policy_rwsem_write(cpu)))
1164 BUG();
1165
1166 retval = __cpufreq_remove_dev(sys_dev);
1167 return retval;
1168}
1169
1170
David Howells65f27f32006-11-22 14:55:48 +00001171static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
David Howells65f27f32006-11-22 14:55:48 +00001173 struct cpufreq_policy *policy =
1174 container_of(work, struct cpufreq_policy, update);
1175 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001176 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 cpufreq_update_policy(cpu);
1178}
1179
1180/**
1181 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
1182 * @cpu: cpu number
1183 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1184 * @new_freq: CPU frequency the CPU actually runs at
1185 *
Dave Jones29464f22009-01-18 01:37:11 -05001186 * We adjust to current frequency first, and need to clean up later.
1187 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301189static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1190 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
1192 struct cpufreq_freqs freqs;
1193
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001194 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1196
1197 freqs.cpu = cpu;
1198 freqs.old = old_freq;
1199 freqs.new = new_freq;
1200 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
1201 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
1202}
1203
1204
Dave Jones32ee8c32006-02-28 00:43:23 -05001205/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301206 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001207 * @cpu: CPU number
1208 *
1209 * This is the last known freq, without actually getting it from the driver.
1210 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1211 */
1212unsigned int cpufreq_quick_get(unsigned int cpu)
1213{
1214 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301215 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001216
1217 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301218 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001219 cpufreq_cpu_put(policy);
1220 }
1221
Dave Jones4d34a672008-02-07 16:33:49 -05001222 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001223}
1224EXPORT_SYMBOL(cpufreq_quick_get);
1225
1226
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001227static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001229 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301230 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001233 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301235 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301237 if (ret_freq && policy->cur &&
1238 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1239 /* verify no discrepancy between actual and
1240 saved value exists */
1241 if (unlikely(ret_freq != policy->cur)) {
1242 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 schedule_work(&policy->update);
1244 }
1245 }
1246
Dave Jones4d34a672008-02-07 16:33:49 -05001247 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001248}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001250/**
1251 * cpufreq_get - get the current CPU frequency (in kHz)
1252 * @cpu: CPU number
1253 *
1254 * Get the CPU current (static) CPU frequency
1255 */
1256unsigned int cpufreq_get(unsigned int cpu)
1257{
1258 unsigned int ret_freq = 0;
1259 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1260
1261 if (!policy)
1262 goto out;
1263
1264 if (unlikely(lock_policy_rwsem_read(cpu)))
1265 goto out_policy;
1266
1267 ret_freq = __cpufreq_get(cpu);
1268
1269 unlock_policy_rwsem_read(cpu);
1270
1271out_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 cpufreq_cpu_put(policy);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001273out:
Dave Jones4d34a672008-02-07 16:33:49 -05001274 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275}
1276EXPORT_SYMBOL(cpufreq_get);
1277
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001278static struct sysdev_driver cpufreq_sysdev_driver = {
1279 .add = cpufreq_add_dev,
1280 .remove = cpufreq_remove_dev,
1281};
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001285 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1286 *
1287 * This function is only executed for the boot processor. The other CPUs
1288 * have been put offline by means of CPU hotplug.
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001289 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001290static int cpufreq_bp_suspend(void)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001291{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301292 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001293
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001294 int cpu = smp_processor_id();
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001295 struct cpufreq_policy *cpu_policy;
1296
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001297 pr_debug("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001298
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001299 /* If there's no policy for the boot CPU, we have nothing to do. */
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001300 cpu_policy = cpufreq_cpu_get(cpu);
1301 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001302 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001303
1304 if (cpufreq_driver->suspend) {
Rafael J. Wysocki7ca64e22011-03-10 21:13:05 +01001305 ret = cpufreq_driver->suspend(cpu_policy);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001306 if (ret)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001307 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1308 "step on CPU %u\n", cpu_policy->cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001309 }
1310
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001311 cpufreq_cpu_put(cpu_policy);
Dave Jonesc9060492008-02-07 16:32:18 -05001312 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001313}
1314
1315/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001316 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 *
1318 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001319 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1320 * restored. It will verify that the current freq is in sync with
1321 * what we believe it to be. This is a bit later than when it
1322 * should be, but nonethteless it's better than calling
1323 * cpufreq_driver->get() here which might re-enable interrupts...
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001324 *
1325 * This function is only executed for the boot CPU. The other CPUs have not
1326 * been turned on yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001328static void cpufreq_bp_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301330 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001331
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001332 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 struct cpufreq_policy *cpu_policy;
1334
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001335 pr_debug("resuming cpu %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001337 /* If there's no policy for the boot CPU, we have nothing to do. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 cpu_policy = cpufreq_cpu_get(cpu);
1339 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001340 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 if (cpufreq_driver->resume) {
1343 ret = cpufreq_driver->resume(cpu_policy);
1344 if (ret) {
1345 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1346 "step on CPU %u\n", cpu_policy->cpu);
Dave Jonesc9060492008-02-07 16:32:18 -05001347 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
1349 }
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 schedule_work(&cpu_policy->update);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001352
Dave Jonesc9060492008-02-07 16:32:18 -05001353fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355}
1356
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001357static struct syscore_ops cpufreq_syscore_ops = {
1358 .suspend = cpufreq_bp_suspend,
1359 .resume = cpufreq_bp_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360};
1361
1362
1363/*********************************************************************
1364 * NOTIFIER LISTS INTERFACE *
1365 *********************************************************************/
1366
1367/**
1368 * cpufreq_register_notifier - register a driver with cpufreq
1369 * @nb: notifier function to register
1370 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1371 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001372 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 * are notified about clock rate changes (once before and once after
1374 * the transition), or a list of drivers that are notified about
1375 * changes in cpufreq policy.
1376 *
1377 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001378 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 */
1380int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1381{
1382 int ret;
1383
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001384 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 switch (list) {
1387 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001388 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001389 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 break;
1391 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001392 ret = blocking_notifier_chain_register(
1393 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 break;
1395 default:
1396 ret = -EINVAL;
1397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 return ret;
1400}
1401EXPORT_SYMBOL(cpufreq_register_notifier);
1402
1403
1404/**
1405 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1406 * @nb: notifier block to be unregistered
1407 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1408 *
1409 * Remove a driver from the CPU frequency notifier list.
1410 *
1411 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001412 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 */
1414int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1415{
1416 int ret;
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 switch (list) {
1419 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001420 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001421 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 break;
1423 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001424 ret = blocking_notifier_chain_unregister(
1425 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 break;
1427 default:
1428 ret = -EINVAL;
1429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 return ret;
1432}
1433EXPORT_SYMBOL(cpufreq_unregister_notifier);
1434
1435
1436/*********************************************************************
1437 * GOVERNORS *
1438 *********************************************************************/
1439
1440
1441int __cpufreq_driver_target(struct cpufreq_policy *policy,
1442 unsigned int target_freq,
1443 unsigned int relation)
1444{
1445 int retval = -EINVAL;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001446
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001447 pr_debug("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 target_freq, relation);
1449 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1450 retval = cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 return retval;
1453}
1454EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456int cpufreq_driver_target(struct cpufreq_policy *policy,
1457 unsigned int target_freq,
1458 unsigned int relation)
1459{
Julia Lawallf1829e42008-07-25 22:44:53 +02001460 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
1462 policy = cpufreq_cpu_get(policy->cpu);
1463 if (!policy)
Julia Lawallf1829e42008-07-25 22:44:53 +02001464 goto no_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001466 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
Julia Lawallf1829e42008-07-25 22:44:53 +02001467 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
1469 ret = __cpufreq_driver_target(policy, target_freq, relation);
1470
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001471 unlock_policy_rwsem_write(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Julia Lawallf1829e42008-07-25 22:44:53 +02001473fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 cpufreq_cpu_put(policy);
Julia Lawallf1829e42008-07-25 22:44:53 +02001475no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 return ret;
1477}
1478EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1479
venkatesh.pallipadi@intel.combf0b90e2008-08-04 11:59:07 -07001480int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu)
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001481{
1482 int ret = 0;
1483
1484 policy = cpufreq_cpu_get(policy->cpu);
1485 if (!policy)
1486 return -EINVAL;
1487
venkatesh.pallipadi@intel.combf0b90e2008-08-04 11:59:07 -07001488 if (cpu_online(cpu) && cpufreq_driver->getavg)
1489 ret = cpufreq_driver->getavg(policy, cpu);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001490
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001491 cpufreq_cpu_put(policy);
1492 return ret;
1493}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001494EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001495
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001496/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001497 * when "event" is CPUFREQ_GOV_LIMITS
1498 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301500static int __cpufreq_governor(struct cpufreq_policy *policy,
1501 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
Dave Jonescc993ca2005-07-28 09:43:56 -07001503 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001504
1505 /* Only must be defined when default governor is known to have latency
1506 restrictions, like e.g. conservative or ondemand.
1507 That this is the case is already ensured in Kconfig
1508 */
1509#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1510 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1511#else
1512 struct cpufreq_governor *gov = NULL;
1513#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001514
1515 if (policy->governor->max_transition_latency &&
1516 policy->cpuinfo.transition_latency >
1517 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07001518 if (!gov)
1519 return -EINVAL;
1520 else {
1521 printk(KERN_WARNING "%s governor failed, too long"
1522 " transition latency of HW, fallback"
1523 " to %s governor\n",
1524 policy->governor->name,
1525 gov->name);
1526 policy->governor = gov;
1527 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530 if (!try_module_get(policy->governor->owner))
1531 return -EINVAL;
1532
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001533 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301534 policy->cpu, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 ret = policy->governor->governor(policy, event);
1536
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301537 /* we keep one module reference alive for
1538 each CPU governed by this CPU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 if ((event != CPUFREQ_GOV_START) || ret)
1540 module_put(policy->governor->owner);
1541 if ((event == CPUFREQ_GOV_STOP) && !ret)
1542 module_put(policy->governor->owner);
1543
1544 return ret;
1545}
1546
1547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548int cpufreq_register_governor(struct cpufreq_governor *governor)
1549{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001550 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
1552 if (!governor)
1553 return -EINVAL;
1554
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001555 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001556
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001557 err = -EBUSY;
1558 if (__find_governor(governor->name) == NULL) {
1559 err = 0;
1560 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Dave Jones32ee8c32006-02-28 00:43:23 -05001563 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001564 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
1566EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1567
1568
1569void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1570{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001571#ifdef CONFIG_HOTPLUG_CPU
1572 int cpu;
1573#endif
1574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 if (!governor)
1576 return;
1577
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001578#ifdef CONFIG_HOTPLUG_CPU
1579 for_each_present_cpu(cpu) {
1580 if (cpu_online(cpu))
1581 continue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001582 if (!strcmp(per_cpu(cpufreq_policy_save, cpu).gov,
1583 governor->name))
1584 strcpy(per_cpu(cpufreq_policy_save, cpu).gov, "\0");
1585 per_cpu(cpufreq_policy_save, cpu).min = 0;
1586 per_cpu(cpufreq_policy_save, cpu).max = 0;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001587 }
1588#endif
1589
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001590 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001592 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 return;
1594}
1595EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1596
1597
1598
1599/*********************************************************************
1600 * POLICY INTERFACE *
1601 *********************************************************************/
1602
1603/**
1604 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05001605 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1606 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 *
1608 * Reads the current cpufreq policy.
1609 */
1610int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1611{
1612 struct cpufreq_policy *cpu_policy;
1613 if (!policy)
1614 return -EINVAL;
1615
1616 cpu_policy = cpufreq_cpu_get(cpu);
1617 if (!cpu_policy)
1618 return -EINVAL;
1619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
1622 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 return 0;
1624}
1625EXPORT_SYMBOL(cpufreq_get_policy);
1626
1627
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001628/*
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301629 * data : current policy.
1630 * policy : policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001631 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301632static int __cpufreq_set_policy(struct cpufreq_policy *data,
1633 struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
1635 int ret = 0;
1636
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001637 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 policy->min, policy->max);
1639
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301640 memcpy(&policy->cpuinfo, &data->cpuinfo,
1641 sizeof(struct cpufreq_cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Yi Yang53391fa2008-01-30 13:33:34 +01001643 if (policy->min > data->max || policy->max < data->min) {
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02001644 ret = -EINVAL;
1645 goto error_out;
1646 }
1647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 /* verify the cpu speed can be set within this limit */
1649 ret = cpufreq_driver->verify(policy);
1650 if (ret)
1651 goto error_out;
1652
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001654 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1655 CPUFREQ_ADJUST, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001658 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1659 CPUFREQ_INCOMPATIBLE, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 /* verify the cpu speed can be set within this limit,
1662 which might be different to the first one */
1663 ret = cpufreq_driver->verify(policy);
Alan Sterne041c682006-03-27 01:16:30 -08001664 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001668 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1669 CPUFREQ_NOTIFY, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Dave Jones7d5e3502006-02-02 17:03:42 -05001671 data->min = policy->min;
1672 data->max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001674 pr_debug("new min and max freqs are %u - %u kHz\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301675 data->min, data->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
1677 if (cpufreq_driver->setpolicy) {
1678 data->policy = policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001679 pr_debug("setting range\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 ret = cpufreq_driver->setpolicy(policy);
1681 } else {
1682 if (policy->governor != data->governor) {
1683 /* save old, working values */
1684 struct cpufreq_governor *old_gov = data->governor;
1685
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001686 pr_debug("governor switch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
1688 /* end old governor */
Andrej Gelenbergffe62752010-05-14 15:15:58 -07001689 if (data->governor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1691
1692 /* start new governor */
1693 data->governor = policy->governor;
1694 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1695 /* new governor failed, so re-start old one */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001696 pr_debug("starting governor %s failed\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301697 data->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 if (old_gov) {
1699 data->governor = old_gov;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301700 __cpufreq_governor(data,
1701 CPUFREQ_GOV_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 }
1703 ret = -EINVAL;
1704 goto error_out;
1705 }
1706 /* might be a policy change, too, so fall through */
1707 }
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001708 pr_debug("governor: change or update limits\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1710 }
1711
Dave Jones7d5e3502006-02-02 17:03:42 -05001712error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 return ret;
1714}
1715
1716/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1718 * @cpu: CPU which shall be re-evaluated
1719 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001720 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 * at different times.
1722 */
1723int cpufreq_update_policy(unsigned int cpu)
1724{
1725 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1726 struct cpufreq_policy policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02001727 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Julia Lawallf1829e42008-07-25 22:44:53 +02001729 if (!data) {
1730 ret = -ENODEV;
1731 goto no_policy;
1732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Julia Lawallf1829e42008-07-25 22:44:53 +02001734 if (unlikely(lock_policy_rwsem_write(cpu))) {
1735 ret = -EINVAL;
1736 goto fail;
1737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001739 pr_debug("updating policy for CPU %u\n", cpu);
Dave Jones7d5e3502006-02-02 17:03:42 -05001740 memcpy(&policy, data, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 policy.min = data->user_policy.min;
1742 policy.max = data->user_policy.max;
1743 policy.policy = data->user_policy.policy;
1744 policy.governor = data->user_policy.governor;
1745
Thomas Renninger0961dd02006-01-26 18:46:33 +01001746 /* BIOS might change freq behind our back
1747 -> ask driver for current freq and notify governors about a change */
1748 if (cpufreq_driver->get) {
1749 policy.cur = cpufreq_driver->get(cpu);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001750 if (!data->cur) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001751 pr_debug("Driver did not initialize current freq");
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001752 data->cur = policy.cur;
1753 } else {
1754 if (data->cur != policy.cur)
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301755 cpufreq_out_of_sync(cpu, data->cur,
1756 policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001757 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01001758 }
1759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 ret = __cpufreq_set_policy(data, &policy);
1761
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001762 unlock_policy_rwsem_write(cpu);
1763
Julia Lawallf1829e42008-07-25 22:44:53 +02001764fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 cpufreq_cpu_put(data);
Julia Lawallf1829e42008-07-25 22:44:53 +02001766no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 return ret;
1768}
1769EXPORT_SYMBOL(cpufreq_update_policy);
1770
Satyam Sharmadd184a02007-10-02 13:28:14 -07001771static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08001772 unsigned long action, void *hcpu)
1773{
1774 unsigned int cpu = (unsigned long)hcpu;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001775 struct sys_device *sys_dev;
1776
1777 sys_dev = get_cpu_sysdev(cpu);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001778 if (sys_dev) {
1779 switch (action) {
1780 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001781 case CPU_ONLINE_FROZEN:
Ashok Rajc32b6b82005-10-30 14:59:54 -08001782 cpufreq_add_dev(sys_dev);
1783 break;
1784 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001785 case CPU_DOWN_PREPARE_FROZEN:
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001786 if (unlikely(lock_policy_rwsem_write(cpu)))
1787 BUG();
1788
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001789 __cpufreq_remove_dev(sys_dev);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001790 break;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001791 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001792 case CPU_DOWN_FAILED_FROZEN:
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001793 cpufreq_add_dev(sys_dev);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001794 break;
1795 }
1796 }
1797 return NOTIFY_OK;
1798}
1799
Neal Buckendahl9c36f742010-06-22 22:02:44 -05001800static struct notifier_block __refdata cpufreq_cpu_notifier = {
Ashok Rajc32b6b82005-10-30 14:59:54 -08001801 .notifier_call = cpufreq_cpu_callback,
1802};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
1804/*********************************************************************
1805 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1806 *********************************************************************/
1807
1808/**
1809 * cpufreq_register_driver - register a CPU Frequency driver
1810 * @driver_data: A struct cpufreq_driver containing the values#
1811 * submitted by the CPU Frequency driver.
1812 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001813 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05001815 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 *
1817 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001818int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
1820 unsigned long flags;
1821 int ret;
1822
1823 if (!driver_data || !driver_data->verify || !driver_data->init ||
1824 ((!driver_data->setpolicy) && (!driver_data->target)))
1825 return -EINVAL;
1826
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001827 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
1829 if (driver_data->setpolicy)
1830 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1831
1832 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1833 if (cpufreq_driver) {
1834 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1835 return -EBUSY;
1836 }
1837 cpufreq_driver = driver_data;
1838 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1839
Mike Travis7a6aedf2008-03-25 15:06:53 -07001840 ret = sysdev_driver_register(&cpu_sysdev_class,
1841 &cpufreq_sysdev_driver);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001842 if (ret)
1843 goto err_null_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001845 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 int i;
1847 ret = -ENODEV;
1848
1849 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07001850 for (i = 0; i < nr_cpu_ids; i++)
1851 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07001853 break;
1854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856 /* if all ->init() calls failed, unregister */
1857 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001858 pr_debug("no CPU initialized for driver %s\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301859 driver_data->name);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001860 goto err_sysdev_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 }
1862 }
1863
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001864 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001865 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001867 return 0;
1868err_sysdev_unreg:
1869 sysdev_driver_unregister(&cpu_sysdev_class,
1870 &cpufreq_sysdev_driver);
1871err_null_driver:
1872 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1873 cpufreq_driver = NULL;
1874 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05001875 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876}
1877EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1878
1879
1880/**
1881 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1882 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001883 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 * the right to do so, i.e. if you have succeeded in initialising before!
1885 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1886 * currently not initialised.
1887 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001888int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
1890 unsigned long flags;
1891
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001892 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001895 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
1897 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001898 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
1900 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1901 cpufreq_driver = NULL;
1902 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1903
1904 return 0;
1905}
1906EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001907
1908static int __init cpufreq_core_init(void)
1909{
1910 int cpu;
1911
1912 for_each_possible_cpu(cpu) {
Tejun Heof1625062009-10-29 22:34:13 +09001913 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001914 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
1915 }
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02001916
1917 cpufreq_global_kobject = kobject_create_and_add("cpufreq",
1918 &cpu_sysdev_class.kset.kobj);
1919 BUG_ON(!cpufreq_global_kobject);
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001920 register_syscore_ops(&cpufreq_syscore_ops);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02001921
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001922 return 0;
1923}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001924core_initcall(cpufreq_core_init);