blob: 7cbf1d53804fda38153315e8dd3cc24b68e57561 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/cpufreq/cpufreq.c
3 *
4 * Copyright (C) 2001 Russell King
5 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
6 *
Ashok Rajc32b6b82005-10-30 14:59:54 -08007 * Oct 2005 - Ashok Raj <ashok.raj@intel.com>
Dave Jones32ee8c32006-02-28 00:43:23 -05008 * Added handling for CPU hotplug
Dave Jones8ff69732006-03-05 03:37:23 -05009 * Feb 2006 - Jacob Shin <jacob.shin@amd.com>
10 * Fix handling for CPU hotplug -- affected CPUs
Ashok Rajc32b6b82005-10-30 14:59:54 -080011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 *
16 */
17
Viresh Kumardb701152012-10-23 01:29:03 +020018#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/notifier.h>
24#include <linux/cpufreq.h>
25#include <linux/delay.h>
26#include <linux/interrupt.h>
27#include <linux/spinlock.h>
28#include <linux/device.h>
29#include <linux/slab.h>
30#include <linux/cpu.h>
31#include <linux/completion.h>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080032#include <linux/mutex.h>
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +010033#include <linux/syscore_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Thomas Renninger6f4f2722010-04-20 13:17:36 +020035#include <trace/events/power.h>
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/**
Dave Jonescd878472006-08-11 17:59:28 -040038 * The "cpufreq driver" - the arch- or hardware-dependent low
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * level driver of CPUFreq support, and its spinlock. This lock
40 * also protects the cpufreq_cpu_data array.
41 */
Dave Jones7d5e3502006-02-02 17:03:42 -050042static struct cpufreq_driver *cpufreq_driver;
Mike Travis7a6aedf2008-03-25 15:06:53 -070043static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
Thomas Renninger084f3492007-07-09 11:35:28 -070044#ifdef CONFIG_HOTPLUG_CPU
45/* This one keeps track of the previously set governor of a removed CPU */
Dmitry Monakhove77b89f2009-10-05 00:38:55 +040046static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
Thomas Renninger084f3492007-07-09 11:35:28 -070047#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static DEFINE_SPINLOCK(cpufreq_driver_lock);
49
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080050/*
51 * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
52 * all cpufreq/hotplug/workqueue/etc related lock issues.
53 *
54 * The rules for this semaphore:
55 * - Any routine that wants to read from the policy structure will
56 * do a down_read on this semaphore.
57 * - Any routine that will write to the policy structure and/or may take away
58 * the policy altogether (eg. CPU hotplug), will hold this lock in write
59 * mode before doing so.
60 *
61 * Additional rules:
62 * - All holders of the lock should check to make sure that the CPU they
63 * are concerned with are online after they get the lock.
64 * - Governor routines that can be called in cpufreq hotplug path should not
65 * take this sem as top level hotplug notifier handler takes this.
Mathieu Desnoyers395913d2009-06-08 13:17:31 -040066 * - Lock should not be held across
67 * __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080068 */
Tejun Heof1625062009-10-29 22:34:13 +090069static DEFINE_PER_CPU(int, cpufreq_policy_cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080070static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
71
72#define lock_policy_rwsem(mode, cpu) \
Viresh Kumarfa1d8af2013-02-07 15:38:42 +053073static int lock_policy_rwsem_##mode(int cpu) \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080074{ \
Tejun Heof1625062009-10-29 22:34:13 +090075 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080076 BUG_ON(policy_cpu == -1); \
77 down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080078 \
79 return 0; \
80}
81
82lock_policy_rwsem(read, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080083lock_policy_rwsem(write, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080084
Viresh Kumarfa1d8af2013-02-07 15:38:42 +053085#define unlock_policy_rwsem(mode, cpu) \
86static void unlock_policy_rwsem_##mode(int cpu) \
87{ \
88 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
89 BUG_ON(policy_cpu == -1); \
90 up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080091}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080092
Viresh Kumarfa1d8af2013-02-07 15:38:42 +053093unlock_policy_rwsem(read, cpu);
94unlock_policy_rwsem(write, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080095
Linus Torvalds1da177e2005-04-16 15:20:36 -070096/* internal prototypes */
Dave Jones29464f22009-01-18 01:37:11 -050097static int __cpufreq_governor(struct cpufreq_policy *policy,
98 unsigned int event);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080099static unsigned int __cpufreq_get(unsigned int cpu);
David Howells65f27f32006-11-22 14:55:48 +0000100static void handle_update(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102/**
Dave Jones32ee8c32006-02-28 00:43:23 -0500103 * Two notifier lists: the "policy" list is involved in the
104 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * "transition" list for kernel code that needs to handle
106 * changes to devices when the CPU clock speed changes.
107 * The mutex locks both lists.
108 */
Alan Sterne041c682006-03-27 01:16:30 -0800109static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700110static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200112static bool init_cpufreq_transition_notifier_list_called;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700113static int __init init_cpufreq_transition_notifier_list(void)
114{
115 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200116 init_cpufreq_transition_notifier_list_called = true;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700117 return 0;
118}
Linus Torvaldsb3438f82006-11-20 11:47:18 -0800119pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400121static int off __read_mostly;
Viresh Kumarda584452012-10-26 00:51:32 +0200122static int cpufreq_disabled(void)
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400123{
124 return off;
125}
126void disable_cpufreq(void)
127{
128 off = 1;
129}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130static LIST_HEAD(cpufreq_governor_list);
Dave Jones29464f22009-01-18 01:37:11 -0500131static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Stephen Boyda9144432012-07-20 18:14:38 +0000133static struct cpufreq_policy *__cpufreq_cpu_get(unsigned int cpu, bool sysfs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
135 struct cpufreq_policy *data;
136 unsigned long flags;
137
Mike Travis7a6aedf2008-03-25 15:06:53 -0700138 if (cpu >= nr_cpu_ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 goto err_out;
140
141 /* get the cpufreq driver */
142 spin_lock_irqsave(&cpufreq_driver_lock, flags);
143
144 if (!cpufreq_driver)
145 goto err_out_unlock;
146
147 if (!try_module_get(cpufreq_driver->owner))
148 goto err_out_unlock;
149
150
151 /* get the CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -0700152 data = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 if (!data)
155 goto err_out_put_module;
156
Stephen Boyda9144432012-07-20 18:14:38 +0000157 if (!sysfs && !kobject_get(&data->kobj))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 goto err_out_put_module;
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return data;
162
Dave Jones7d5e3502006-02-02 17:03:42 -0500163err_out_put_module:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 module_put(cpufreq_driver->owner);
Dave Jones7d5e3502006-02-02 17:03:42 -0500165err_out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones7d5e3502006-02-02 17:03:42 -0500167err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return NULL;
169}
Stephen Boyda9144432012-07-20 18:14:38 +0000170
171struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
172{
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000173 if (cpufreq_disabled())
174 return NULL;
175
Stephen Boyda9144432012-07-20 18:14:38 +0000176 return __cpufreq_cpu_get(cpu, false);
177}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
179
Stephen Boyda9144432012-07-20 18:14:38 +0000180static struct cpufreq_policy *cpufreq_cpu_get_sysfs(unsigned int cpu)
181{
182 return __cpufreq_cpu_get(cpu, true);
183}
184
185static void __cpufreq_cpu_put(struct cpufreq_policy *data, bool sysfs)
186{
187 if (!sysfs)
188 kobject_put(&data->kobj);
189 module_put(cpufreq_driver->owner);
190}
Dave Jones7d5e3502006-02-02 17:03:42 -0500191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192void cpufreq_cpu_put(struct cpufreq_policy *data)
193{
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000194 if (cpufreq_disabled())
195 return;
196
Stephen Boyda9144432012-07-20 18:14:38 +0000197 __cpufreq_cpu_put(data, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
200
Stephen Boyda9144432012-07-20 18:14:38 +0000201static void cpufreq_cpu_put_sysfs(struct cpufreq_policy *data)
202{
203 __cpufreq_cpu_put(data, true);
204}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
208 *********************************************************************/
209
210/**
211 * adjust_jiffies - adjust the system "loops_per_jiffy"
212 *
213 * This function alters the system "loops_per_jiffy" for the clock
214 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500215 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 * per-CPU loops_per_jiffy value wherever possible.
217 */
218#ifndef CONFIG_SMP
219static unsigned long l_p_j_ref;
220static unsigned int l_p_j_ref_freq;
221
Arjan van de Ven858119e2006-01-14 13:20:43 -0800222static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 if (ci->flags & CPUFREQ_CONST_LOOPS)
225 return;
226
227 if (!l_p_j_ref_freq) {
228 l_p_j_ref = loops_per_jiffy;
229 l_p_j_ref_freq = ci->old;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200230 pr_debug("saving %lu as reference value for loops_per_jiffy; "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530231 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
Afzal Mohammedd08de0c12012-01-04 10:52:46 +0530233 if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700234 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530235 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
236 ci->new);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200237 pr_debug("scaling loops_per_jiffy to %lu "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530238 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240}
241#else
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530242static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
243{
244 return;
245}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246#endif
247
248
249/**
Dave Jonese4472cb2006-01-31 15:53:55 -0800250 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
251 * on frequency transition.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 *
Dave Jonese4472cb2006-01-31 15:53:55 -0800253 * This function calls the transition notifiers and the "adjust_jiffies"
254 * function. It is called twice on all CPU frequency changes that have
Dave Jones32ee8c32006-02-28 00:43:23 -0500255 * external effects.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 */
257void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
258{
Dave Jonese4472cb2006-01-31 15:53:55 -0800259 struct cpufreq_policy *policy;
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 BUG_ON(irqs_disabled());
262
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000263 if (cpufreq_disabled())
264 return;
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200267 pr_debug("notification %u of frequency transition to %u kHz\n",
Dave Jonese4472cb2006-01-31 15:53:55 -0800268 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Mike Travis7a6aedf2008-03-25 15:06:53 -0700270 policy = per_cpu(cpufreq_cpu_data, freqs->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500274 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800275 * which is not equal to what the cpufreq core thinks is
276 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 */
278 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800279 if ((policy) && (policy->cpu == freqs->cpu) &&
280 (policy->cur) && (policy->cur != freqs->old)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200281 pr_debug("Warning: CPU frequency is"
Dave Jonese4472cb2006-01-31 15:53:55 -0800282 " %u, cpufreq assumed %u kHz.\n",
283 freqs->old, policy->cur);
284 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700287 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800288 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
290 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 case CPUFREQ_POSTCHANGE:
293 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200294 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
Thomas Renninger6f4f2722010-04-20 13:17:36 +0200295 (unsigned long)freqs->cpu);
296 trace_power_frequency(POWER_PSTATE, freqs->new, freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100297 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700298 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800299 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800300 if (likely(policy) && likely(policy->cpu == freqs->cpu))
301 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 break;
303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
306
307
308
309/*********************************************************************
310 * SYSFS INTERFACE *
311 *********************************************************************/
312
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700313static struct cpufreq_governor *__find_governor(const char *str_governor)
314{
315 struct cpufreq_governor *t;
316
317 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
Dave Jones29464f22009-01-18 01:37:11 -0500318 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700319 return t;
320
321 return NULL;
322}
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324/**
325 * cpufreq_parse_governor - parse a governor string
326 */
Dave Jones905d77c2008-03-05 14:28:32 -0500327static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 struct cpufreq_governor **governor)
329{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700330 int err = -EINVAL;
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700333 goto out;
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (cpufreq_driver->setpolicy) {
336 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
337 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700338 err = 0;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530339 } else if (!strnicmp(str_governor, "powersave",
340 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700342 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700344 } else if (cpufreq_driver->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700346
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800347 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700348
349 t = __find_governor(str_governor);
350
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700351 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700352 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700353
Kees Cook1a8e1462011-05-04 08:38:56 -0700354 mutex_unlock(&cpufreq_governor_mutex);
355 ret = request_module("cpufreq_%s", str_governor);
356 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700357
Kees Cook1a8e1462011-05-04 08:38:56 -0700358 if (ret == 0)
359 t = __find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700360 }
361
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700362 if (t != NULL) {
363 *governor = t;
364 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700366
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800367 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
Dave Jones29464f22009-01-18 01:37:11 -0500369out:
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700370 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530375 * cpufreq_per_cpu_attr_read() / show_##file_name() -
376 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 *
378 * Write out information from cpufreq_driver->policy[cpu]; object must be
379 * "unsigned int".
380 */
381
Dave Jones32ee8c32006-02-28 00:43:23 -0500382#define show_one(file_name, object) \
383static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500384(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500385{ \
Dave Jones29464f22009-01-18 01:37:11 -0500386 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
389show_one(cpuinfo_min_freq, cpuinfo.min_freq);
390show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100391show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392show_one(scaling_min_freq, min);
393show_one(scaling_max_freq, max);
394show_one(scaling_cur_freq, cur);
395
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530396static int __cpufreq_set_policy(struct cpufreq_policy *data,
397 struct cpufreq_policy *policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399/**
400 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
401 */
402#define store_one(file_name, object) \
403static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500404(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{ \
Jingoo Hanf55c9c22012-10-31 05:49:13 +0000406 unsigned int ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 struct cpufreq_policy new_policy; \
408 \
409 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
410 if (ret) \
411 return -EINVAL; \
412 \
Dave Jones29464f22009-01-18 01:37:11 -0500413 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (ret != 1) \
415 return -EINVAL; \
416 \
Thomas Renninger7970e082006-04-13 15:14:04 +0200417 ret = __cpufreq_set_policy(policy, &new_policy); \
418 policy->user_policy.object = policy->object; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 \
420 return ret ? ret : count; \
421}
422
Dave Jones29464f22009-01-18 01:37:11 -0500423store_one(scaling_min_freq, min);
424store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426/**
427 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
428 */
Dave Jones905d77c2008-03-05 14:28:32 -0500429static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
430 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800432 unsigned int cur_freq = __cpufreq_get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (!cur_freq)
434 return sprintf(buf, "<unknown>");
435 return sprintf(buf, "%u\n", cur_freq);
436}
437
438
439/**
440 * show_scaling_governor - show the current policy for the specified CPU
441 */
Dave Jones905d77c2008-03-05 14:28:32 -0500442static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Dave Jones29464f22009-01-18 01:37:11 -0500444 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return sprintf(buf, "powersave\n");
446 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
447 return sprintf(buf, "performance\n");
448 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200449 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500450 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return -EINVAL;
452}
453
454
455/**
456 * store_scaling_governor - store policy for the specified CPU
457 */
Dave Jones905d77c2008-03-05 14:28:32 -0500458static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
459 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Jingoo Hanf55c9c22012-10-31 05:49:13 +0000461 unsigned int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 char str_governor[16];
463 struct cpufreq_policy new_policy;
464
465 ret = cpufreq_get_policy(&new_policy, policy->cpu);
466 if (ret)
467 return ret;
468
Dave Jones29464f22009-01-18 01:37:11 -0500469 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (ret != 1)
471 return -EINVAL;
472
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530473 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
474 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return -EINVAL;
476
Thomas Renninger7970e082006-04-13 15:14:04 +0200477 /* Do not use cpufreq_set_policy here or the user_policy.max
478 will be wrongly overridden */
Thomas Renninger7970e082006-04-13 15:14:04 +0200479 ret = __cpufreq_set_policy(policy, &new_policy);
480
481 policy->user_policy.policy = policy->policy;
482 policy->user_policy.governor = policy->governor;
Thomas Renninger7970e082006-04-13 15:14:04 +0200483
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530484 if (ret)
485 return ret;
486 else
487 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
490/**
491 * show_scaling_driver - show the cpufreq driver currently loaded
492 */
Dave Jones905d77c2008-03-05 14:28:32 -0500493static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
viresh kumar4b972f02012-10-23 01:23:43 +0200495 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
498/**
499 * show_scaling_available_governors - show the available CPUfreq governors
500 */
Dave Jones905d77c2008-03-05 14:28:32 -0500501static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
502 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
504 ssize_t i = 0;
505 struct cpufreq_governor *t;
506
507 if (!cpufreq_driver->target) {
508 i += sprintf(buf, "performance powersave");
509 goto out;
510 }
511
512 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
Dave Jones29464f22009-01-18 01:37:11 -0500513 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
514 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200516 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500518out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 i += sprintf(&buf[i], "\n");
520 return i;
521}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700522
Rusty Russell835481d2009-01-04 05:18:06 -0800523static ssize_t show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
525 ssize_t i = 0;
526 unsigned int cpu;
527
Rusty Russell835481d2009-01-04 05:18:06 -0800528 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (i)
530 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
531 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
532 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500533 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
535 i += sprintf(&buf[i], "\n");
536 return i;
537}
538
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700539/**
540 * show_related_cpus - show the CPUs affected by each transition even if
541 * hw coordination is in use
542 */
543static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
544{
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700545 return show_cpus(policy->related_cpus, buf);
546}
547
548/**
549 * show_affected_cpus - show the CPUs affected by each transition
550 */
551static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
552{
553 return show_cpus(policy->cpus, buf);
554}
555
Venki Pallipadi9e769882007-10-26 10:18:21 -0700556static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500557 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700558{
559 unsigned int freq = 0;
560 unsigned int ret;
561
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700562 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700563 return -EINVAL;
564
565 ret = sscanf(buf, "%u", &freq);
566 if (ret != 1)
567 return -EINVAL;
568
569 policy->governor->store_setspeed(policy, freq);
570
571 return count;
572}
573
574static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
575{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700576 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700577 return sprintf(buf, "<unsupported>\n");
578
579 return policy->governor->show_setspeed(policy, buf);
580}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Thomas Renningere2f74f32009-11-19 12:31:01 +0100582/**
viresh kumar8bf1ac722012-10-23 01:23:33 +0200583 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100584 */
585static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
586{
587 unsigned int limit;
588 int ret;
589 if (cpufreq_driver->bios_limit) {
590 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
591 if (!ret)
592 return sprintf(buf, "%u\n", limit);
593 }
594 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
595}
596
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200597cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
598cpufreq_freq_attr_ro(cpuinfo_min_freq);
599cpufreq_freq_attr_ro(cpuinfo_max_freq);
600cpufreq_freq_attr_ro(cpuinfo_transition_latency);
601cpufreq_freq_attr_ro(scaling_available_governors);
602cpufreq_freq_attr_ro(scaling_driver);
603cpufreq_freq_attr_ro(scaling_cur_freq);
604cpufreq_freq_attr_ro(bios_limit);
605cpufreq_freq_attr_ro(related_cpus);
606cpufreq_freq_attr_ro(affected_cpus);
607cpufreq_freq_attr_rw(scaling_min_freq);
608cpufreq_freq_attr_rw(scaling_max_freq);
609cpufreq_freq_attr_rw(scaling_governor);
610cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Dave Jones905d77c2008-03-05 14:28:32 -0500612static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 &cpuinfo_min_freq.attr,
614 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100615 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 &scaling_min_freq.attr,
617 &scaling_max_freq.attr,
618 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700619 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 &scaling_governor.attr,
621 &scaling_driver.attr,
622 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700623 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 NULL
625};
626
Thomas Renninger8aa84ad2009-07-24 15:25:05 +0200627struct kobject *cpufreq_global_kobject;
628EXPORT_SYMBOL(cpufreq_global_kobject);
629
Dave Jones29464f22009-01-18 01:37:11 -0500630#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
631#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Dave Jones29464f22009-01-18 01:37:11 -0500633static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Dave Jones905d77c2008-03-05 14:28:32 -0500635 struct cpufreq_policy *policy = to_policy(kobj);
636 struct freq_attr *fattr = to_attr(attr);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500637 ssize_t ret = -EINVAL;
Stephen Boyda9144432012-07-20 18:14:38 +0000638 policy = cpufreq_cpu_get_sysfs(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (!policy)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500640 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800641
642 if (lock_policy_rwsem_read(policy->cpu) < 0)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500643 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800644
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530645 if (fattr->show)
646 ret = fattr->show(policy, buf);
647 else
648 ret = -EIO;
649
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800650 unlock_policy_rwsem_read(policy->cpu);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500651fail:
Stephen Boyda9144432012-07-20 18:14:38 +0000652 cpufreq_cpu_put_sysfs(policy);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500653no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return ret;
655}
656
Dave Jones905d77c2008-03-05 14:28:32 -0500657static ssize_t store(struct kobject *kobj, struct attribute *attr,
658 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Dave Jones905d77c2008-03-05 14:28:32 -0500660 struct cpufreq_policy *policy = to_policy(kobj);
661 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500662 ssize_t ret = -EINVAL;
Stephen Boyda9144432012-07-20 18:14:38 +0000663 policy = cpufreq_cpu_get_sysfs(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (!policy)
Dave Jonesa07530b2008-03-05 14:22:25 -0500665 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800666
667 if (lock_policy_rwsem_write(policy->cpu) < 0)
Dave Jonesa07530b2008-03-05 14:22:25 -0500668 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800669
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530670 if (fattr->store)
671 ret = fattr->store(policy, buf, count);
672 else
673 ret = -EIO;
674
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800675 unlock_policy_rwsem_write(policy->cpu);
Dave Jonesa07530b2008-03-05 14:22:25 -0500676fail:
Stephen Boyda9144432012-07-20 18:14:38 +0000677 cpufreq_cpu_put_sysfs(policy);
Dave Jonesa07530b2008-03-05 14:22:25 -0500678no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return ret;
680}
681
Dave Jones905d77c2008-03-05 14:28:32 -0500682static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Dave Jones905d77c2008-03-05 14:28:32 -0500684 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200685 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 complete(&policy->kobj_unregister);
687}
688
Emese Revfy52cf25d2010-01-19 02:58:23 +0100689static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 .show = show,
691 .store = store,
692};
693
694static struct kobj_type ktype_cpufreq = {
695 .sysfs_ops = &sysfs_ops,
696 .default_attrs = default_attrs,
697 .release = cpufreq_sysfs_release,
698};
699
Dave Jones19d6f7e2009-07-08 17:35:39 -0400700/* symlink affected CPUs */
Alex Chiangcf3289d02009-11-17 20:27:08 -0700701static int cpufreq_add_dev_symlink(unsigned int cpu,
702 struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400703{
704 unsigned int j;
705 int ret = 0;
706
707 for_each_cpu(j, policy->cpus) {
708 struct cpufreq_policy *managed_policy;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800709 struct device *cpu_dev;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400710
711 if (j == cpu)
712 continue;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400713
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200714 pr_debug("CPU %u already managed, adding link\n", j);
Dave Jones19d6f7e2009-07-08 17:35:39 -0400715 managed_policy = cpufreq_cpu_get(cpu);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800716 cpu_dev = get_cpu_device(j);
717 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Dave Jones19d6f7e2009-07-08 17:35:39 -0400718 "cpufreq");
719 if (ret) {
720 cpufreq_cpu_put(managed_policy);
721 return ret;
722 }
723 }
724 return ret;
725}
726
Alex Chiangcf3289d02009-11-17 20:27:08 -0700727static int cpufreq_add_dev_interface(unsigned int cpu,
728 struct cpufreq_policy *policy,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800729 struct device *dev)
Dave Jones909a6942009-07-08 18:05:42 -0400730{
Dave Jonesecf7e462009-07-08 18:48:47 -0400731 struct cpufreq_policy new_policy;
Dave Jones909a6942009-07-08 18:05:42 -0400732 struct freq_attr **drv_attr;
733 unsigned long flags;
734 int ret = 0;
735 unsigned int j;
736
737 /* prepare interface data */
738 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800739 &dev->kobj, "cpufreq");
Dave Jones909a6942009-07-08 18:05:42 -0400740 if (ret)
741 return ret;
742
743 /* set up files for this cpu device */
744 drv_attr = cpufreq_driver->attr;
745 while ((drv_attr) && (*drv_attr)) {
746 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
747 if (ret)
748 goto err_out_kobj_put;
749 drv_attr++;
750 }
751 if (cpufreq_driver->get) {
752 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
753 if (ret)
754 goto err_out_kobj_put;
755 }
756 if (cpufreq_driver->target) {
757 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
758 if (ret)
759 goto err_out_kobj_put;
760 }
Thomas Renningere2f74f32009-11-19 12:31:01 +0100761 if (cpufreq_driver->bios_limit) {
762 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
763 if (ret)
764 goto err_out_kobj_put;
765 }
Dave Jones909a6942009-07-08 18:05:42 -0400766
767 spin_lock_irqsave(&cpufreq_driver_lock, flags);
768 for_each_cpu(j, policy->cpus) {
Dave Jones909a6942009-07-08 18:05:42 -0400769 per_cpu(cpufreq_cpu_data, j) = policy;
Tejun Heof1625062009-10-29 22:34:13 +0900770 per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
Dave Jones909a6942009-07-08 18:05:42 -0400771 }
772 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
773
774 ret = cpufreq_add_dev_symlink(cpu, policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400775 if (ret)
776 goto err_out_kobj_put;
777
778 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
779 /* assure that the starting sequence is run in __cpufreq_set_policy */
780 policy->governor = NULL;
781
782 /* set default policy */
783 ret = __cpufreq_set_policy(policy, &new_policy);
784 policy->user_policy.policy = policy->policy;
785 policy->user_policy.governor = policy->governor;
786
787 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200788 pr_debug("setting policy failed\n");
Dave Jonesecf7e462009-07-08 18:48:47 -0400789 if (cpufreq_driver->exit)
790 cpufreq_driver->exit(policy);
791 }
Dave Jones909a6942009-07-08 18:05:42 -0400792 return ret;
793
794err_out_kobj_put:
795 kobject_put(&policy->kobj);
796 wait_for_completion(&policy->kobj_unregister);
797 return ret;
798}
799
Viresh Kumarfcf80582013-01-29 14:39:08 +0000800#ifdef CONFIG_HOTPLUG_CPU
801static int cpufreq_add_policy_cpu(unsigned int cpu, unsigned int sibling,
802 struct device *dev)
803{
804 struct cpufreq_policy *policy;
805 int ret = 0;
806 unsigned long flags;
807
808 policy = cpufreq_cpu_get(sibling);
809 WARN_ON(!policy);
810
811 per_cpu(cpufreq_policy_cpu, cpu) = policy->cpu;
812
813 lock_policy_rwsem_write(cpu);
814
815 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
816
817 spin_lock_irqsave(&cpufreq_driver_lock, flags);
818 cpumask_set_cpu(cpu, policy->cpus);
819 per_cpu(cpufreq_cpu_data, cpu) = policy;
820 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
821
822 __cpufreq_governor(policy, CPUFREQ_GOV_START);
823 __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
824
825 unlock_policy_rwsem_write(cpu);
826
827 ret = sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
828 if (ret) {
829 cpufreq_cpu_put(policy);
830 return ret;
831 }
832
833 return 0;
834}
835#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837/**
838 * cpufreq_add_dev - add a CPU device
839 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500840 * Adds the cpufreq interface for a CPU device.
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400841 *
842 * The Oracle says: try running cpufreq registration/unregistration concurrently
843 * with with cpu hotplugging and all hell will break loose. Tried to clean this
844 * mess up, but more thorough testing is needed. - Mathieu
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800846static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Viresh Kumarfcf80582013-01-29 14:39:08 +0000848 unsigned int j, cpu = dev->id;
Viresh Kumar65922462013-02-07 10:56:03 +0530849 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 unsigned long flags;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500852#ifdef CONFIG_HOTPLUG_CPU
Viresh Kumarfcf80582013-01-29 14:39:08 +0000853 struct cpufreq_governor *gov;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500854 int sibling;
855#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Ashok Rajc32b6b82005-10-30 14:59:54 -0800857 if (cpu_is_offline(cpu))
858 return 0;
859
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200860 pr_debug("adding CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862#ifdef CONFIG_SMP
863 /* check whether a different CPU already registered this
864 * CPU because it is in the same boat. */
865 policy = cpufreq_cpu_get(cpu);
866 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -0500867 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return 0;
869 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000870
871#ifdef CONFIG_HOTPLUG_CPU
872 /* Check if this cpu was hot-unplugged earlier and has siblings */
873 for_each_online_cpu(sibling) {
874 struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
875 if (cp && cpumask_test_cpu(cpu, cp->related_cpus))
876 return cpufreq_add_policy_cpu(cpu, sibling, dev);
877 }
878#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879#endif
880
881 if (!try_module_get(cpufreq_driver->owner)) {
882 ret = -EINVAL;
883 goto module_out;
884 }
885
Dave Jonese98df502005-10-20 15:17:43 -0700886 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
Dave Jones059019a2009-07-08 16:30:03 -0400887 if (!policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 goto nomem_out;
Dave Jones059019a2009-07-08 16:30:03 -0400889
890 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400891 goto err_free_policy;
Dave Jones059019a2009-07-08 16:30:03 -0400892
893 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400894 goto err_free_cpumask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 policy->cpu = cpu;
Viresh Kumar65922462013-02-07 10:56:03 +0530897 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Rusty Russell835481d2009-01-04 05:18:06 -0800898 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800900 /* Initially set CPU itself as the policy_cpu */
Tejun Heof1625062009-10-29 22:34:13 +0900901 per_cpu(cpufreq_policy_cpu, cpu) = cpu;
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400902 ret = (lock_policy_rwsem_write(cpu) < 0);
903 WARN_ON(ret);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 init_completion(&policy->kobj_unregister);
David Howells65f27f32006-11-22 14:55:48 +0000906 INIT_WORK(&policy->update, handle_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 /* call driver. From then on the cpufreq must be able
909 * to accept all calls to ->verify and ->setpolicy for this CPU
910 */
911 ret = cpufreq_driver->init(policy);
912 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200913 pr_debug("initialization failed\n");
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400914 goto err_unlock_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
Viresh Kumar643ae6e2013-01-12 05:14:38 +0000916
Viresh Kumarfcf80582013-01-29 14:39:08 +0000917 /* related cpus should atleast have policy->cpus */
918 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
919
Viresh Kumar643ae6e2013-01-12 05:14:38 +0000920 /*
921 * affected cpus must always be the one, which are online. We aren't
922 * managing offline cpus here.
923 */
924 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
925
Mike Chan187d9f42008-12-04 12:19:17 -0800926 policy->user_policy.min = policy->min;
927 policy->user_policy.max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Thomas Renningera1531ac2008-07-29 22:32:58 -0700929 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
930 CPUFREQ_START, policy);
931
Viresh Kumarfcf80582013-01-29 14:39:08 +0000932#ifdef CONFIG_HOTPLUG_CPU
933 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
934 if (gov) {
935 policy->governor = gov;
936 pr_debug("Restoring governor %s for cpu %d\n",
937 policy->governor->name, cpu);
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200938 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000939#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800941 ret = cpufreq_add_dev_interface(cpu, policy, dev);
Dave Jones19d6f7e2009-07-08 17:35:39 -0400942 if (ret)
943 goto err_out_unregister;
Dave Jones8ff69732006-03-05 03:37:23 -0500944
Lothar Waßmanndca02612008-05-29 17:54:52 +0200945 unlock_policy_rwsem_write(cpu);
946
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -0400947 kobject_uevent(&policy->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 module_put(cpufreq_driver->owner);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200949 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -0500950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 return 0;
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953err_out_unregister:
954 spin_lock_irqsave(&cpufreq_driver_lock, flags);
Rusty Russell835481d2009-01-04 05:18:06 -0800955 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -0700956 per_cpu(cpufreq_cpu_data, j) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
958
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800959 kobject_put(&policy->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 wait_for_completion(&policy->kobj_unregister);
961
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400962err_unlock_policy:
Dave Jones45709112008-03-05 14:07:34 -0500963 unlock_policy_rwsem_write(cpu);
Xiaotian Fengcad70a62010-07-20 20:11:02 +0800964 free_cpumask_var(policy->related_cpus);
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400965err_free_cpumask:
966 free_cpumask_var(policy->cpus);
967err_free_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 kfree(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969nomem_out:
970 module_put(cpufreq_driver->owner);
Ashok Rajc32b6b82005-10-30 14:59:54 -0800971module_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 return ret;
973}
974
Viresh Kumarb8eed8a2013-01-14 13:23:03 +0000975static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
976{
977 int j;
978
979 policy->last_cpu = policy->cpu;
980 policy->cpu = cpu;
981
Viresh Kumar3361b7b2013-02-04 11:38:51 +0000982 for_each_cpu(j, policy->cpus)
Viresh Kumarb8eed8a2013-01-14 13:23:03 +0000983 per_cpu(cpufreq_policy_cpu, j) = cpu;
Viresh Kumarb8eed8a2013-01-14 13:23:03 +0000984
985#ifdef CONFIG_CPU_FREQ_TABLE
986 cpufreq_frequency_table_update_policy_cpu(policy);
987#endif
988 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
989 CPUFREQ_UPDATE_POLICY_CPU, policy);
990}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992/**
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800993 * __cpufreq_remove_dev - remove a CPU device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 *
995 * Removes the cpufreq interface for a CPU device.
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800996 * Caller should already have policy_rwsem in write mode for this CPU.
997 * This routine frees the rwsem before returning.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800999static int __cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001001 unsigned int cpu = dev->id, ret, cpus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 unsigned long flags;
1003 struct cpufreq_policy *data;
Amerigo Wang499bca92010-03-04 03:23:46 -05001004 struct kobject *kobj;
1005 struct completion *cmp;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001006 struct device *cpu_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001008 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 spin_lock_irqsave(&cpufreq_driver_lock, flags);
Mike Travis7a6aedf2008-03-25 15:06:53 -07001011 data = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 if (!data) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001014 pr_debug("%s: No cpu_data found\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001016 unlock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return -EINVAL;
1018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001020 if (cpufreq_driver->target)
Viresh Kumarf6a74092013-01-12 05:14:39 +00001021 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Thomas Renninger084f3492007-07-09 11:35:28 -07001022
1023#ifdef CONFIG_HOTPLUG_CPU
Dmitry Monakhove77b89f2009-10-05 00:38:55 +04001024 strncpy(per_cpu(cpufreq_cpu_governor, cpu), data->governor->name,
1025 CPUFREQ_NAME_LEN);
Thomas Renninger084f3492007-07-09 11:35:28 -07001026#endif
1027
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001028 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1029 cpus = cpumask_weight(data->cpus);
1030 cpumask_clear_cpu(cpu, data->cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Viresh Kumar73bf0fc2013-02-05 22:21:14 +01001032 if (cpu != data->cpu) {
1033 sysfs_remove_link(&dev->kobj, "cpufreq");
1034 } else if (cpus > 1) {
Jacob Shin27ecddc2011-04-27 13:32:11 -05001035 /* first sibling now owns the new sysfs dir */
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001036 cpu_dev = get_cpu_device(cpumask_first(data->cpus));
1037 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
1038 ret = kobject_move(&data->kobj, &cpu_dev->kobj);
1039 if (ret) {
1040 pr_err("%s: Failed to move kobj: %d", __func__, ret);
1041 cpumask_set_cpu(cpu, data->cpus);
1042 ret = sysfs_create_link(&cpu_dev->kobj, &data->kobj,
1043 "cpufreq");
1044 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1045 unlock_policy_rwsem_write(cpu);
1046 return -EINVAL;
1047 }
Jacob Shin27ecddc2011-04-27 13:32:11 -05001048
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001049 update_policy_cpu(data, cpu_dev->id);
1050 pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
1051 __func__, cpu_dev->id, cpu);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001052 }
Jacob Shin27ecddc2011-04-27 13:32:11 -05001053
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001054 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1055
1056 pr_debug("%s: removing link, cpu: %d\n", __func__, cpu);
1057 cpufreq_cpu_put(data);
1058 unlock_policy_rwsem_write(cpu);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001059
1060 /* If cpu is last user of policy, free policy */
1061 if (cpus == 1) {
1062 lock_policy_rwsem_write(cpu);
1063 kobj = &data->kobj;
1064 cmp = &data->kobj_unregister;
1065 unlock_policy_rwsem_write(cpu);
1066 kobject_put(kobj);
1067
1068 /* we need to make sure that the underlying kobj is actually
1069 * not referenced anymore by anybody before we proceed with
1070 * unloading.
1071 */
1072 pr_debug("waiting for dropping of refcount\n");
1073 wait_for_completion(cmp);
1074 pr_debug("wait complete\n");
1075
1076 lock_policy_rwsem_write(cpu);
1077 if (cpufreq_driver->exit)
1078 cpufreq_driver->exit(data);
1079 unlock_policy_rwsem_write(cpu);
1080
1081 free_cpumask_var(data->related_cpus);
1082 free_cpumask_var(data->cpus);
1083 kfree(data);
1084 } else if (cpufreq_driver->target) {
1085 __cpufreq_governor(data, CPUFREQ_GOV_START);
1086 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 return 0;
1090}
1091
1092
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001093static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001094{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001095 unsigned int cpu = dev->id;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001096 int retval;
Venki Pallipadiec282972007-03-26 12:03:19 -07001097
1098 if (cpu_is_offline(cpu))
1099 return 0;
1100
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001101 if (unlikely(lock_policy_rwsem_write(cpu)))
1102 BUG();
1103
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001104 retval = __cpufreq_remove_dev(dev, sif);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001105 return retval;
1106}
1107
1108
David Howells65f27f32006-11-22 14:55:48 +00001109static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
David Howells65f27f32006-11-22 14:55:48 +00001111 struct cpufreq_policy *policy =
1112 container_of(work, struct cpufreq_policy, update);
1113 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001114 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 cpufreq_update_policy(cpu);
1116}
1117
1118/**
1119 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
1120 * @cpu: cpu number
1121 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1122 * @new_freq: CPU frequency the CPU actually runs at
1123 *
Dave Jones29464f22009-01-18 01:37:11 -05001124 * We adjust to current frequency first, and need to clean up later.
1125 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301127static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1128 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
1130 struct cpufreq_freqs freqs;
1131
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001132 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1134
1135 freqs.cpu = cpu;
1136 freqs.old = old_freq;
1137 freqs.new = new_freq;
1138 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
1139 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
1140}
1141
1142
Dave Jones32ee8c32006-02-28 00:43:23 -05001143/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301144 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001145 * @cpu: CPU number
1146 *
1147 * This is the last known freq, without actually getting it from the driver.
1148 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1149 */
1150unsigned int cpufreq_quick_get(unsigned int cpu)
1151{
1152 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301153 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001154
1155 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301156 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001157 cpufreq_cpu_put(policy);
1158 }
1159
Dave Jones4d34a672008-02-07 16:33:49 -05001160 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001161}
1162EXPORT_SYMBOL(cpufreq_quick_get);
1163
Jesse Barnes3d737102011-06-28 10:59:12 -07001164/**
1165 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1166 * @cpu: CPU number
1167 *
1168 * Just return the max possible frequency for a given CPU.
1169 */
1170unsigned int cpufreq_quick_get_max(unsigned int cpu)
1171{
1172 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1173 unsigned int ret_freq = 0;
1174
1175 if (policy) {
1176 ret_freq = policy->max;
1177 cpufreq_cpu_put(policy);
1178 }
1179
1180 return ret_freq;
1181}
1182EXPORT_SYMBOL(cpufreq_quick_get_max);
1183
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001184
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001185static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001187 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301188 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001191 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301193 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301195 if (ret_freq && policy->cur &&
1196 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1197 /* verify no discrepancy between actual and
1198 saved value exists */
1199 if (unlikely(ret_freq != policy->cur)) {
1200 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 schedule_work(&policy->update);
1202 }
1203 }
1204
Dave Jones4d34a672008-02-07 16:33:49 -05001205 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001206}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001208/**
1209 * cpufreq_get - get the current CPU frequency (in kHz)
1210 * @cpu: CPU number
1211 *
1212 * Get the CPU current (static) CPU frequency
1213 */
1214unsigned int cpufreq_get(unsigned int cpu)
1215{
1216 unsigned int ret_freq = 0;
1217 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1218
1219 if (!policy)
1220 goto out;
1221
1222 if (unlikely(lock_policy_rwsem_read(cpu)))
1223 goto out_policy;
1224
1225 ret_freq = __cpufreq_get(cpu);
1226
1227 unlock_policy_rwsem_read(cpu);
1228
1229out_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 cpufreq_cpu_put(policy);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001231out:
Dave Jones4d34a672008-02-07 16:33:49 -05001232 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233}
1234EXPORT_SYMBOL(cpufreq_get);
1235
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001236static struct subsys_interface cpufreq_interface = {
1237 .name = "cpufreq",
1238 .subsys = &cpu_subsys,
1239 .add_dev = cpufreq_add_dev,
1240 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001241};
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001245 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1246 *
1247 * This function is only executed for the boot processor. The other CPUs
1248 * have been put offline by means of CPU hotplug.
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001249 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001250static int cpufreq_bp_suspend(void)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001251{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301252 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001253
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001254 int cpu = smp_processor_id();
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001255 struct cpufreq_policy *cpu_policy;
1256
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001257 pr_debug("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001258
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001259 /* If there's no policy for the boot CPU, we have nothing to do. */
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001260 cpu_policy = cpufreq_cpu_get(cpu);
1261 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001262 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001263
1264 if (cpufreq_driver->suspend) {
Rafael J. Wysocki7ca64e22011-03-10 21:13:05 +01001265 ret = cpufreq_driver->suspend(cpu_policy);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001266 if (ret)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001267 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1268 "step on CPU %u\n", cpu_policy->cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001269 }
1270
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001271 cpufreq_cpu_put(cpu_policy);
Dave Jonesc9060492008-02-07 16:32:18 -05001272 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001273}
1274
1275/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001276 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 *
1278 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001279 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1280 * restored. It will verify that the current freq is in sync with
1281 * what we believe it to be. This is a bit later than when it
1282 * should be, but nonethteless it's better than calling
1283 * cpufreq_driver->get() here which might re-enable interrupts...
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001284 *
1285 * This function is only executed for the boot CPU. The other CPUs have not
1286 * been turned on yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001288static void cpufreq_bp_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301290 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001291
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001292 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 struct cpufreq_policy *cpu_policy;
1294
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001295 pr_debug("resuming cpu %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001297 /* If there's no policy for the boot CPU, we have nothing to do. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 cpu_policy = cpufreq_cpu_get(cpu);
1299 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001300 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 if (cpufreq_driver->resume) {
1303 ret = cpufreq_driver->resume(cpu_policy);
1304 if (ret) {
1305 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1306 "step on CPU %u\n", cpu_policy->cpu);
Dave Jonesc9060492008-02-07 16:32:18 -05001307 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
1309 }
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 schedule_work(&cpu_policy->update);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001312
Dave Jonesc9060492008-02-07 16:32:18 -05001313fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315}
1316
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001317static struct syscore_ops cpufreq_syscore_ops = {
1318 .suspend = cpufreq_bp_suspend,
1319 .resume = cpufreq_bp_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320};
1321
Borislav Petkov9d950462013-01-20 10:24:28 +00001322/**
1323 * cpufreq_get_current_driver - return current driver's name
1324 *
1325 * Return the name string of the currently loaded cpufreq driver
1326 * or NULL, if none.
1327 */
1328const char *cpufreq_get_current_driver(void)
1329{
1330 if (cpufreq_driver)
1331 return cpufreq_driver->name;
1332
1333 return NULL;
1334}
1335EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337/*********************************************************************
1338 * NOTIFIER LISTS INTERFACE *
1339 *********************************************************************/
1340
1341/**
1342 * cpufreq_register_notifier - register a driver with cpufreq
1343 * @nb: notifier function to register
1344 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1345 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001346 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 * are notified about clock rate changes (once before and once after
1348 * the transition), or a list of drivers that are notified about
1349 * changes in cpufreq policy.
1350 *
1351 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001352 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 */
1354int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1355{
1356 int ret;
1357
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001358 if (cpufreq_disabled())
1359 return -EINVAL;
1360
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001361 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 switch (list) {
1364 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001365 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001366 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 break;
1368 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001369 ret = blocking_notifier_chain_register(
1370 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 break;
1372 default:
1373 ret = -EINVAL;
1374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 return ret;
1377}
1378EXPORT_SYMBOL(cpufreq_register_notifier);
1379
1380
1381/**
1382 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1383 * @nb: notifier block to be unregistered
1384 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1385 *
1386 * Remove a driver from the CPU frequency notifier list.
1387 *
1388 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001389 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 */
1391int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1392{
1393 int ret;
1394
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001395 if (cpufreq_disabled())
1396 return -EINVAL;
1397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 switch (list) {
1399 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001400 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001401 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 break;
1403 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001404 ret = blocking_notifier_chain_unregister(
1405 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 break;
1407 default:
1408 ret = -EINVAL;
1409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 return ret;
1412}
1413EXPORT_SYMBOL(cpufreq_unregister_notifier);
1414
1415
1416/*********************************************************************
1417 * GOVERNORS *
1418 *********************************************************************/
1419
1420
1421int __cpufreq_driver_target(struct cpufreq_policy *policy,
1422 unsigned int target_freq,
1423 unsigned int relation)
1424{
1425 int retval = -EINVAL;
Viresh Kumar72499242012-10-31 01:28:21 +01001426 unsigned int old_target_freq = target_freq;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001427
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001428 if (cpufreq_disabled())
1429 return -ENODEV;
1430
Viresh Kumar72499242012-10-31 01:28:21 +01001431 /* Make sure that target_freq is within supported range */
1432 if (target_freq > policy->max)
1433 target_freq = policy->max;
1434 if (target_freq < policy->min)
1435 target_freq = policy->min;
1436
1437 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1438 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001439
1440 if (target_freq == policy->cur)
1441 return 0;
1442
Viresh Kumar3361b7b2013-02-04 11:38:51 +00001443 if (cpufreq_driver->target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 retval = cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return retval;
1447}
1448EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450int cpufreq_driver_target(struct cpufreq_policy *policy,
1451 unsigned int target_freq,
1452 unsigned int relation)
1453{
Julia Lawallf1829e42008-07-25 22:44:53 +02001454 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456 policy = cpufreq_cpu_get(policy->cpu);
1457 if (!policy)
Julia Lawallf1829e42008-07-25 22:44:53 +02001458 goto no_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001460 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
Julia Lawallf1829e42008-07-25 22:44:53 +02001461 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 ret = __cpufreq_driver_target(policy, target_freq, relation);
1464
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001465 unlock_policy_rwsem_write(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Julia Lawallf1829e42008-07-25 22:44:53 +02001467fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 cpufreq_cpu_put(policy);
Julia Lawallf1829e42008-07-25 22:44:53 +02001469no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return ret;
1471}
1472EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1473
venkatesh.pallipadi@intel.combf0b90e2008-08-04 11:59:07 -07001474int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu)
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001475{
1476 int ret = 0;
1477
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001478 if (cpufreq_disabled())
1479 return ret;
1480
Viresh Kumar3361b7b2013-02-04 11:38:51 +00001481 if (!cpufreq_driver->getavg)
Viresh Kumar0676f7f2012-10-24 23:39:48 +02001482 return 0;
1483
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001484 policy = cpufreq_cpu_get(policy->cpu);
1485 if (!policy)
1486 return -EINVAL;
1487
Viresh Kumar0676f7f2012-10-24 23:39:48 +02001488 ret = cpufreq_driver->getavg(policy, cpu);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001489
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001490 cpufreq_cpu_put(policy);
1491 return ret;
1492}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001493EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001494
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001495/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001496 * when "event" is CPUFREQ_GOV_LIMITS
1497 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301499static int __cpufreq_governor(struct cpufreq_policy *policy,
1500 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
Dave Jonescc993ca2005-07-28 09:43:56 -07001502 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001503
1504 /* Only must be defined when default governor is known to have latency
1505 restrictions, like e.g. conservative or ondemand.
1506 That this is the case is already ensured in Kconfig
1507 */
1508#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1509 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1510#else
1511 struct cpufreq_governor *gov = NULL;
1512#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001513
1514 if (policy->governor->max_transition_latency &&
1515 policy->cpuinfo.transition_latency >
1516 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07001517 if (!gov)
1518 return -EINVAL;
1519 else {
1520 printk(KERN_WARNING "%s governor failed, too long"
1521 " transition latency of HW, fallback"
1522 " to %s governor\n",
1523 policy->governor->name,
1524 gov->name);
1525 policy->governor = gov;
1526 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 if (!try_module_get(policy->governor->owner))
1530 return -EINVAL;
1531
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001532 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301533 policy->cpu, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 ret = policy->governor->governor(policy, event);
1535
Viresh Kumar8e536952013-02-07 12:51:27 +05301536 if (event == CPUFREQ_GOV_START)
1537 policy->governor->initialized++;
1538 else if (event == CPUFREQ_GOV_STOP)
1539 policy->governor->initialized--;
Viresh Kumarb3940582013-02-01 05:42:58 +00001540
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301541 /* we keep one module reference alive for
1542 each CPU governed by this CPU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 if ((event != CPUFREQ_GOV_START) || ret)
1544 module_put(policy->governor->owner);
1545 if ((event == CPUFREQ_GOV_STOP) && !ret)
1546 module_put(policy->governor->owner);
1547
1548 return ret;
1549}
1550
1551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552int cpufreq_register_governor(struct cpufreq_governor *governor)
1553{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001554 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
1556 if (!governor)
1557 return -EINVAL;
1558
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001559 if (cpufreq_disabled())
1560 return -ENODEV;
1561
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001562 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001563
Viresh Kumarb3940582013-02-01 05:42:58 +00001564 governor->initialized = 0;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001565 err = -EBUSY;
1566 if (__find_governor(governor->name) == NULL) {
1567 err = 0;
1568 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Dave Jones32ee8c32006-02-28 00:43:23 -05001571 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001572 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573}
1574EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1575
1576
1577void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1578{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001579#ifdef CONFIG_HOTPLUG_CPU
1580 int cpu;
1581#endif
1582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 if (!governor)
1584 return;
1585
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001586 if (cpufreq_disabled())
1587 return;
1588
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001589#ifdef CONFIG_HOTPLUG_CPU
1590 for_each_present_cpu(cpu) {
1591 if (cpu_online(cpu))
1592 continue;
1593 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1594 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1595 }
1596#endif
1597
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001598 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001600 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 return;
1602}
1603EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1604
1605
1606
1607/*********************************************************************
1608 * POLICY INTERFACE *
1609 *********************************************************************/
1610
1611/**
1612 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05001613 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1614 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 *
1616 * Reads the current cpufreq policy.
1617 */
1618int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1619{
1620 struct cpufreq_policy *cpu_policy;
1621 if (!policy)
1622 return -EINVAL;
1623
1624 cpu_policy = cpufreq_cpu_get(cpu);
1625 if (!cpu_policy)
1626 return -EINVAL;
1627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
1630 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 return 0;
1632}
1633EXPORT_SYMBOL(cpufreq_get_policy);
1634
1635
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001636/*
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301637 * data : current policy.
1638 * policy : policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001639 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301640static int __cpufreq_set_policy(struct cpufreq_policy *data,
1641 struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642{
1643 int ret = 0;
1644
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001645 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 policy->min, policy->max);
1647
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301648 memcpy(&policy->cpuinfo, &data->cpuinfo,
1649 sizeof(struct cpufreq_cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Yi Yang53391fa2008-01-30 13:33:34 +01001651 if (policy->min > data->max || policy->max < data->min) {
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02001652 ret = -EINVAL;
1653 goto error_out;
1654 }
1655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 /* verify the cpu speed can be set within this limit */
1657 ret = cpufreq_driver->verify(policy);
1658 if (ret)
1659 goto error_out;
1660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001662 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1663 CPUFREQ_ADJUST, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001666 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1667 CPUFREQ_INCOMPATIBLE, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
1669 /* verify the cpu speed can be set within this limit,
1670 which might be different to the first one */
1671 ret = cpufreq_driver->verify(policy);
Alan Sterne041c682006-03-27 01:16:30 -08001672 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001676 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1677 CPUFREQ_NOTIFY, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Dave Jones7d5e3502006-02-02 17:03:42 -05001679 data->min = policy->min;
1680 data->max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001682 pr_debug("new min and max freqs are %u - %u kHz\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301683 data->min, data->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685 if (cpufreq_driver->setpolicy) {
1686 data->policy = policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001687 pr_debug("setting range\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 ret = cpufreq_driver->setpolicy(policy);
1689 } else {
1690 if (policy->governor != data->governor) {
1691 /* save old, working values */
1692 struct cpufreq_governor *old_gov = data->governor;
1693
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001694 pr_debug("governor switch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
1696 /* end old governor */
Andrej Gelenbergffe62752010-05-14 15:15:58 -07001697 if (data->governor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1699
1700 /* start new governor */
1701 data->governor = policy->governor;
1702 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1703 /* new governor failed, so re-start old one */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001704 pr_debug("starting governor %s failed\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301705 data->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 if (old_gov) {
1707 data->governor = old_gov;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301708 __cpufreq_governor(data,
1709 CPUFREQ_GOV_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 }
1711 ret = -EINVAL;
1712 goto error_out;
1713 }
1714 /* might be a policy change, too, so fall through */
1715 }
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001716 pr_debug("governor: change or update limits\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1718 }
1719
Dave Jones7d5e3502006-02-02 17:03:42 -05001720error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 return ret;
1722}
1723
1724/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1726 * @cpu: CPU which shall be re-evaluated
1727 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001728 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 * at different times.
1730 */
1731int cpufreq_update_policy(unsigned int cpu)
1732{
1733 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1734 struct cpufreq_policy policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02001735 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Julia Lawallf1829e42008-07-25 22:44:53 +02001737 if (!data) {
1738 ret = -ENODEV;
1739 goto no_policy;
1740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Julia Lawallf1829e42008-07-25 22:44:53 +02001742 if (unlikely(lock_policy_rwsem_write(cpu))) {
1743 ret = -EINVAL;
1744 goto fail;
1745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001747 pr_debug("updating policy for CPU %u\n", cpu);
Dave Jones7d5e3502006-02-02 17:03:42 -05001748 memcpy(&policy, data, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 policy.min = data->user_policy.min;
1750 policy.max = data->user_policy.max;
1751 policy.policy = data->user_policy.policy;
1752 policy.governor = data->user_policy.governor;
1753
Thomas Renninger0961dd02006-01-26 18:46:33 +01001754 /* BIOS might change freq behind our back
1755 -> ask driver for current freq and notify governors about a change */
1756 if (cpufreq_driver->get) {
1757 policy.cur = cpufreq_driver->get(cpu);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001758 if (!data->cur) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001759 pr_debug("Driver did not initialize current freq");
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001760 data->cur = policy.cur;
1761 } else {
1762 if (data->cur != policy.cur)
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301763 cpufreq_out_of_sync(cpu, data->cur,
1764 policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001765 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01001766 }
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 ret = __cpufreq_set_policy(data, &policy);
1769
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001770 unlock_policy_rwsem_write(cpu);
1771
Julia Lawallf1829e42008-07-25 22:44:53 +02001772fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 cpufreq_cpu_put(data);
Julia Lawallf1829e42008-07-25 22:44:53 +02001774no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 return ret;
1776}
1777EXPORT_SYMBOL(cpufreq_update_policy);
1778
Satyam Sharmadd184a02007-10-02 13:28:14 -07001779static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08001780 unsigned long action, void *hcpu)
1781{
1782 unsigned int cpu = (unsigned long)hcpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001783 struct device *dev;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001784
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001785 dev = get_cpu_device(cpu);
1786 if (dev) {
Ashok Rajc32b6b82005-10-30 14:59:54 -08001787 switch (action) {
1788 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001789 case CPU_ONLINE_FROZEN:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001790 cpufreq_add_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001791 break;
1792 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001793 case CPU_DOWN_PREPARE_FROZEN:
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001794 if (unlikely(lock_policy_rwsem_write(cpu)))
1795 BUG();
1796
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001797 __cpufreq_remove_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001798 break;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001799 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001800 case CPU_DOWN_FAILED_FROZEN:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001801 cpufreq_add_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001802 break;
1803 }
1804 }
1805 return NOTIFY_OK;
1806}
1807
Neal Buckendahl9c36f742010-06-22 22:02:44 -05001808static struct notifier_block __refdata cpufreq_cpu_notifier = {
Ashok Rajc32b6b82005-10-30 14:59:54 -08001809 .notifier_call = cpufreq_cpu_callback,
1810};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
1812/*********************************************************************
1813 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1814 *********************************************************************/
1815
1816/**
1817 * cpufreq_register_driver - register a CPU Frequency driver
1818 * @driver_data: A struct cpufreq_driver containing the values#
1819 * submitted by the CPU Frequency driver.
1820 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001821 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05001823 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 *
1825 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001826int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
1828 unsigned long flags;
1829 int ret;
1830
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001831 if (cpufreq_disabled())
1832 return -ENODEV;
1833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 if (!driver_data || !driver_data->verify || !driver_data->init ||
1835 ((!driver_data->setpolicy) && (!driver_data->target)))
1836 return -EINVAL;
1837
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001838 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
1840 if (driver_data->setpolicy)
1841 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1842
1843 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1844 if (cpufreq_driver) {
1845 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1846 return -EBUSY;
1847 }
1848 cpufreq_driver = driver_data;
1849 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1850
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001851 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001852 if (ret)
1853 goto err_null_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001855 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 int i;
1857 ret = -ENODEV;
1858
1859 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07001860 for (i = 0; i < nr_cpu_ids; i++)
1861 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07001863 break;
1864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
1866 /* if all ->init() calls failed, unregister */
1867 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001868 pr_debug("no CPU initialized for driver %s\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301869 driver_data->name);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001870 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 }
1872 }
1873
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001874 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001875 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001877 return 0;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001878err_if_unreg:
1879 subsys_interface_unregister(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001880err_null_driver:
1881 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1882 cpufreq_driver = NULL;
1883 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05001884 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885}
1886EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1887
1888
1889/**
1890 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1891 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001892 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 * the right to do so, i.e. if you have succeeded in initialising before!
1894 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1895 * currently not initialised.
1896 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001897int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898{
1899 unsigned long flags;
1900
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001901 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001904 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001906 subsys_interface_unregister(&cpufreq_interface);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001907 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
1909 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1910 cpufreq_driver = NULL;
1911 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1912
1913 return 0;
1914}
1915EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001916
1917static int __init cpufreq_core_init(void)
1918{
1919 int cpu;
1920
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001921 if (cpufreq_disabled())
1922 return -ENODEV;
1923
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001924 for_each_possible_cpu(cpu) {
Tejun Heof1625062009-10-29 22:34:13 +09001925 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001926 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
1927 }
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02001928
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001929 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02001930 BUG_ON(!cpufreq_global_kobject);
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001931 register_syscore_ops(&cpufreq_syscore_ops);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02001932
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001933 return 0;
1934}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001935core_initcall(cpufreq_core_init);