blob: 3faf62bdbad0143dd85b576da8a9d3530dda6fe1 [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 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +020042static 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
Nathan Zimmer0d1857a2013-02-22 16:24:34 +000048static DEFINE_RWLOCK(cpufreq_driver_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
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:
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080062 * - Governor routines that can be called in cpufreq hotplug path should not
63 * take this sem as top level hotplug notifier handler takes this.
Mathieu Desnoyers395913d2009-06-08 13:17:31 -040064 * - Lock should not be held across
65 * __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080066 */
Tejun Heof1625062009-10-29 22:34:13 +090067static DEFINE_PER_CPU(int, cpufreq_policy_cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080068static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
69
70#define lock_policy_rwsem(mode, cpu) \
Viresh Kumarfa1d8af2013-02-07 15:38:42 +053071static int lock_policy_rwsem_##mode(int cpu) \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080072{ \
Tejun Heof1625062009-10-29 22:34:13 +090073 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080074 BUG_ON(policy_cpu == -1); \
75 down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080076 \
77 return 0; \
78}
79
80lock_policy_rwsem(read, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080081lock_policy_rwsem(write, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080082
Viresh Kumarfa1d8af2013-02-07 15:38:42 +053083#define unlock_policy_rwsem(mode, cpu) \
84static void unlock_policy_rwsem_##mode(int cpu) \
85{ \
86 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
87 BUG_ON(policy_cpu == -1); \
88 up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080089}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080090
Viresh Kumarfa1d8af2013-02-07 15:38:42 +053091unlock_policy_rwsem(read, cpu);
92unlock_policy_rwsem(write, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/* internal prototypes */
Dave Jones29464f22009-01-18 01:37:11 -050095static int __cpufreq_governor(struct cpufreq_policy *policy,
96 unsigned int event);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080097static unsigned int __cpufreq_get(unsigned int cpu);
David Howells65f27f32006-11-22 14:55:48 +000098static void handle_update(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100/**
Dave Jones32ee8c32006-02-28 00:43:23 -0500101 * Two notifier lists: the "policy" list is involved in the
102 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 * "transition" list for kernel code that needs to handle
104 * changes to devices when the CPU clock speed changes.
105 * The mutex locks both lists.
106 */
Alan Sterne041c682006-03-27 01:16:30 -0800107static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700108static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200110static bool init_cpufreq_transition_notifier_list_called;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700111static int __init init_cpufreq_transition_notifier_list(void)
112{
113 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200114 init_cpufreq_transition_notifier_list_called = true;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700115 return 0;
116}
Linus Torvaldsb3438f82006-11-20 11:47:18 -0800117pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400119static int off __read_mostly;
Viresh Kumarda584452012-10-26 00:51:32 +0200120static int cpufreq_disabled(void)
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400121{
122 return off;
123}
124void disable_cpufreq(void)
125{
126 off = 1;
127}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128static LIST_HEAD(cpufreq_governor_list);
Dave Jones29464f22009-01-18 01:37:11 -0500129static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000131bool have_governor_per_policy(void)
132{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200133 return cpufreq_driver->have_governor_per_policy;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000134}
Viresh Kumar3f869d62013-05-16 05:09:56 +0000135EXPORT_SYMBOL_GPL(have_governor_per_policy);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000136
Viresh Kumar944e9a02013-05-16 05:09:57 +0000137struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
138{
139 if (have_governor_per_policy())
140 return &policy->kobj;
141 else
142 return cpufreq_global_kobject;
143}
144EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
145
Stephen Boyda9144432012-07-20 18:14:38 +0000146static struct cpufreq_policy *__cpufreq_cpu_get(unsigned int cpu, bool sysfs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 struct cpufreq_policy *data;
149 unsigned long flags;
150
Mike Travis7a6aedf2008-03-25 15:06:53 -0700151 if (cpu >= nr_cpu_ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 goto err_out;
153
154 /* get the cpufreq driver */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000155 read_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200157 if (!cpufreq_driver)
158 goto err_out_unlock;
159
160 if (!try_module_get(cpufreq_driver->owner))
161 goto err_out_unlock;
162
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 /* get the CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -0700165 data = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 if (!data)
168 goto err_out_put_module;
169
Stephen Boyda9144432012-07-20 18:14:38 +0000170 if (!sysfs && !kobject_get(&data->kobj))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 goto err_out_put_module;
172
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000173 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return data;
175
Dave Jones7d5e3502006-02-02 17:03:42 -0500176err_out_put_module:
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200177 module_put(cpufreq_driver->owner);
Nathan Zimmer58000432013-04-04 14:53:25 +0000178err_out_unlock:
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200179 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones7d5e3502006-02-02 17:03:42 -0500180err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return NULL;
182}
Stephen Boyda9144432012-07-20 18:14:38 +0000183
184struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
185{
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000186 if (cpufreq_disabled())
187 return NULL;
188
Stephen Boyda9144432012-07-20 18:14:38 +0000189 return __cpufreq_cpu_get(cpu, false);
190}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
192
Stephen Boyda9144432012-07-20 18:14:38 +0000193static struct cpufreq_policy *cpufreq_cpu_get_sysfs(unsigned int cpu)
194{
195 return __cpufreq_cpu_get(cpu, true);
196}
197
198static void __cpufreq_cpu_put(struct cpufreq_policy *data, bool sysfs)
199{
200 if (!sysfs)
201 kobject_put(&data->kobj);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200202 module_put(cpufreq_driver->owner);
Stephen Boyda9144432012-07-20 18:14:38 +0000203}
Dave Jones7d5e3502006-02-02 17:03:42 -0500204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205void cpufreq_cpu_put(struct cpufreq_policy *data)
206{
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000207 if (cpufreq_disabled())
208 return;
209
Stephen Boyda9144432012-07-20 18:14:38 +0000210 __cpufreq_cpu_put(data, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
213
Stephen Boyda9144432012-07-20 18:14:38 +0000214static void cpufreq_cpu_put_sysfs(struct cpufreq_policy *data)
215{
216 __cpufreq_cpu_put(data, true);
217}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
221 *********************************************************************/
222
223/**
224 * adjust_jiffies - adjust the system "loops_per_jiffy"
225 *
226 * This function alters the system "loops_per_jiffy" for the clock
227 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500228 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 * per-CPU loops_per_jiffy value wherever possible.
230 */
231#ifndef CONFIG_SMP
232static unsigned long l_p_j_ref;
233static unsigned int l_p_j_ref_freq;
234
Arjan van de Ven858119e2006-01-14 13:20:43 -0800235static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 if (ci->flags & CPUFREQ_CONST_LOOPS)
238 return;
239
240 if (!l_p_j_ref_freq) {
241 l_p_j_ref = loops_per_jiffy;
242 l_p_j_ref_freq = ci->old;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200243 pr_debug("saving %lu as reference value for loops_per_jiffy; "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530244 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
Afzal Mohammedd08de0c12012-01-04 10:52:46 +0530246 if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700247 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530248 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
249 ci->new);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200250 pr_debug("scaling loops_per_jiffy to %lu "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530251 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253}
254#else
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530255static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
256{
257 return;
258}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259#endif
260
261
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530262void __cpufreq_notify_transition(struct cpufreq_policy *policy,
263 struct cpufreq_freqs *freqs, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 BUG_ON(irqs_disabled());
266
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000267 if (cpufreq_disabled())
268 return;
269
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200270 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200271 pr_debug("notification %u of frequency transition to %u kHz\n",
Dave Jonese4472cb2006-01-31 15:53:55 -0800272 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500277 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800278 * which is not equal to what the cpufreq core thinks is
279 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200281 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800282 if ((policy) && (policy->cpu == freqs->cpu) &&
283 (policy->cur) && (policy->cur != freqs->old)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200284 pr_debug("Warning: CPU frequency is"
Dave Jonese4472cb2006-01-31 15:53:55 -0800285 " %u, cpufreq assumed %u kHz.\n",
286 freqs->old, policy->cur);
287 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700290 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800291 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
293 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 case CPUFREQ_POSTCHANGE:
296 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200297 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
Thomas Renninger6f4f2722010-04-20 13:17:36 +0200298 (unsigned long)freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100299 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700300 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800301 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800302 if (likely(policy) && likely(policy->cpu == freqs->cpu))
303 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 break;
305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530307/**
308 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
309 * on frequency transition.
310 *
311 * This function calls the transition notifiers and the "adjust_jiffies"
312 * function. It is called twice on all CPU frequency changes that have
313 * external effects.
314 */
315void cpufreq_notify_transition(struct cpufreq_policy *policy,
316 struct cpufreq_freqs *freqs, unsigned int state)
317{
318 for_each_cpu(freqs->cpu, policy->cpus)
319 __cpufreq_notify_transition(policy, freqs, state);
320}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
322
323
324
325/*********************************************************************
326 * SYSFS INTERFACE *
327 *********************************************************************/
328
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700329static struct cpufreq_governor *__find_governor(const char *str_governor)
330{
331 struct cpufreq_governor *t;
332
333 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
Dave Jones29464f22009-01-18 01:37:11 -0500334 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700335 return t;
336
337 return NULL;
338}
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340/**
341 * cpufreq_parse_governor - parse a governor string
342 */
Dave Jones905d77c2008-03-05 14:28:32 -0500343static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 struct cpufreq_governor **governor)
345{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700346 int err = -EINVAL;
347
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200348 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700349 goto out;
350
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200351 if (cpufreq_driver->setpolicy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
353 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700354 err = 0;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530355 } else if (!strnicmp(str_governor, "powersave",
356 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700358 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200360 } else if (cpufreq_driver->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700362
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800363 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700364
365 t = __find_governor(str_governor);
366
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700367 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700368 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700369
Kees Cook1a8e1462011-05-04 08:38:56 -0700370 mutex_unlock(&cpufreq_governor_mutex);
371 ret = request_module("cpufreq_%s", str_governor);
372 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700373
Kees Cook1a8e1462011-05-04 08:38:56 -0700374 if (ret == 0)
375 t = __find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700376 }
377
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700378 if (t != NULL) {
379 *governor = t;
380 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700382
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800383 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
Dave Jones29464f22009-01-18 01:37:11 -0500385out:
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700386 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530391 * cpufreq_per_cpu_attr_read() / show_##file_name() -
392 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 *
394 * Write out information from cpufreq_driver->policy[cpu]; object must be
395 * "unsigned int".
396 */
397
Dave Jones32ee8c32006-02-28 00:43:23 -0500398#define show_one(file_name, object) \
399static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500400(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500401{ \
Dave Jones29464f22009-01-18 01:37:11 -0500402 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
405show_one(cpuinfo_min_freq, cpuinfo.min_freq);
406show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100407show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408show_one(scaling_min_freq, min);
409show_one(scaling_max_freq, max);
410show_one(scaling_cur_freq, cur);
411
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530412static int __cpufreq_set_policy(struct cpufreq_policy *data,
413 struct cpufreq_policy *policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415/**
416 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
417 */
418#define store_one(file_name, object) \
419static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500420(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{ \
Jingoo Hanf55c9c22012-10-31 05:49:13 +0000422 unsigned int ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 struct cpufreq_policy new_policy; \
424 \
425 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
426 if (ret) \
427 return -EINVAL; \
428 \
Dave Jones29464f22009-01-18 01:37:11 -0500429 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (ret != 1) \
431 return -EINVAL; \
432 \
Thomas Renninger7970e082006-04-13 15:14:04 +0200433 ret = __cpufreq_set_policy(policy, &new_policy); \
434 policy->user_policy.object = policy->object; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 \
436 return ret ? ret : count; \
437}
438
Dave Jones29464f22009-01-18 01:37:11 -0500439store_one(scaling_min_freq, min);
440store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442/**
443 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
444 */
Dave Jones905d77c2008-03-05 14:28:32 -0500445static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
446 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800448 unsigned int cur_freq = __cpufreq_get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (!cur_freq)
450 return sprintf(buf, "<unknown>");
451 return sprintf(buf, "%u\n", cur_freq);
452}
453
454
455/**
456 * show_scaling_governor - show the current policy for the specified CPU
457 */
Dave Jones905d77c2008-03-05 14:28:32 -0500458static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Dave Jones29464f22009-01-18 01:37:11 -0500460 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return sprintf(buf, "powersave\n");
462 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
463 return sprintf(buf, "performance\n");
464 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200465 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500466 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return -EINVAL;
468}
469
470
471/**
472 * store_scaling_governor - store policy for the specified CPU
473 */
Dave Jones905d77c2008-03-05 14:28:32 -0500474static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
475 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Jingoo Hanf55c9c22012-10-31 05:49:13 +0000477 unsigned int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 char str_governor[16];
479 struct cpufreq_policy new_policy;
480
481 ret = cpufreq_get_policy(&new_policy, policy->cpu);
482 if (ret)
483 return ret;
484
Dave Jones29464f22009-01-18 01:37:11 -0500485 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (ret != 1)
487 return -EINVAL;
488
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530489 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
490 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return -EINVAL;
492
Thomas Renninger7970e082006-04-13 15:14:04 +0200493 /* Do not use cpufreq_set_policy here or the user_policy.max
494 will be wrongly overridden */
Thomas Renninger7970e082006-04-13 15:14:04 +0200495 ret = __cpufreq_set_policy(policy, &new_policy);
496
497 policy->user_policy.policy = policy->policy;
498 policy->user_policy.governor = policy->governor;
Thomas Renninger7970e082006-04-13 15:14:04 +0200499
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530500 if (ret)
501 return ret;
502 else
503 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
506/**
507 * show_scaling_driver - show the cpufreq driver currently loaded
508 */
Dave Jones905d77c2008-03-05 14:28:32 -0500509static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200511 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
514/**
515 * show_scaling_available_governors - show the available CPUfreq governors
516 */
Dave Jones905d77c2008-03-05 14:28:32 -0500517static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
518 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 ssize_t i = 0;
521 struct cpufreq_governor *t;
522
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200523 if (!cpufreq_driver->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 i += sprintf(buf, "performance powersave");
525 goto out;
526 }
527
528 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
Dave Jones29464f22009-01-18 01:37:11 -0500529 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
530 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200532 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500534out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 i += sprintf(&buf[i], "\n");
536 return i;
537}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700538
Rusty Russell835481d2009-01-04 05:18:06 -0800539static ssize_t show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
541 ssize_t i = 0;
542 unsigned int cpu;
543
Rusty Russell835481d2009-01-04 05:18:06 -0800544 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (i)
546 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
547 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
548 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500549 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
551 i += sprintf(&buf[i], "\n");
552 return i;
553}
554
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700555/**
556 * show_related_cpus - show the CPUs affected by each transition even if
557 * hw coordination is in use
558 */
559static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
560{
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700561 return show_cpus(policy->related_cpus, buf);
562}
563
564/**
565 * show_affected_cpus - show the CPUs affected by each transition
566 */
567static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
568{
569 return show_cpus(policy->cpus, buf);
570}
571
Venki Pallipadi9e769882007-10-26 10:18:21 -0700572static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500573 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700574{
575 unsigned int freq = 0;
576 unsigned int ret;
577
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700578 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700579 return -EINVAL;
580
581 ret = sscanf(buf, "%u", &freq);
582 if (ret != 1)
583 return -EINVAL;
584
585 policy->governor->store_setspeed(policy, freq);
586
587 return count;
588}
589
590static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
591{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700592 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700593 return sprintf(buf, "<unsupported>\n");
594
595 return policy->governor->show_setspeed(policy, buf);
596}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Thomas Renningere2f74f32009-11-19 12:31:01 +0100598/**
viresh kumar8bf1ac722012-10-23 01:23:33 +0200599 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100600 */
601static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
602{
603 unsigned int limit;
604 int ret;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200605 if (cpufreq_driver->bios_limit) {
606 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
Thomas Renningere2f74f32009-11-19 12:31:01 +0100607 if (!ret)
608 return sprintf(buf, "%u\n", limit);
609 }
610 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
611}
612
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200613cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
614cpufreq_freq_attr_ro(cpuinfo_min_freq);
615cpufreq_freq_attr_ro(cpuinfo_max_freq);
616cpufreq_freq_attr_ro(cpuinfo_transition_latency);
617cpufreq_freq_attr_ro(scaling_available_governors);
618cpufreq_freq_attr_ro(scaling_driver);
619cpufreq_freq_attr_ro(scaling_cur_freq);
620cpufreq_freq_attr_ro(bios_limit);
621cpufreq_freq_attr_ro(related_cpus);
622cpufreq_freq_attr_ro(affected_cpus);
623cpufreq_freq_attr_rw(scaling_min_freq);
624cpufreq_freq_attr_rw(scaling_max_freq);
625cpufreq_freq_attr_rw(scaling_governor);
626cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Dave Jones905d77c2008-03-05 14:28:32 -0500628static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 &cpuinfo_min_freq.attr,
630 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100631 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 &scaling_min_freq.attr,
633 &scaling_max_freq.attr,
634 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700635 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 &scaling_governor.attr,
637 &scaling_driver.attr,
638 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700639 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 NULL
641};
642
Thomas Renninger8aa84ad2009-07-24 15:25:05 +0200643struct kobject *cpufreq_global_kobject;
644EXPORT_SYMBOL(cpufreq_global_kobject);
645
Dave Jones29464f22009-01-18 01:37:11 -0500646#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
647#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Dave Jones29464f22009-01-18 01:37:11 -0500649static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Dave Jones905d77c2008-03-05 14:28:32 -0500651 struct cpufreq_policy *policy = to_policy(kobj);
652 struct freq_attr *fattr = to_attr(attr);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500653 ssize_t ret = -EINVAL;
Stephen Boyda9144432012-07-20 18:14:38 +0000654 policy = cpufreq_cpu_get_sysfs(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (!policy)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500656 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800657
658 if (lock_policy_rwsem_read(policy->cpu) < 0)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500659 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800660
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530661 if (fattr->show)
662 ret = fattr->show(policy, buf);
663 else
664 ret = -EIO;
665
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800666 unlock_policy_rwsem_read(policy->cpu);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500667fail:
Stephen Boyda9144432012-07-20 18:14:38 +0000668 cpufreq_cpu_put_sysfs(policy);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500669no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return ret;
671}
672
Dave Jones905d77c2008-03-05 14:28:32 -0500673static ssize_t store(struct kobject *kobj, struct attribute *attr,
674 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Dave Jones905d77c2008-03-05 14:28:32 -0500676 struct cpufreq_policy *policy = to_policy(kobj);
677 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500678 ssize_t ret = -EINVAL;
Stephen Boyda9144432012-07-20 18:14:38 +0000679 policy = cpufreq_cpu_get_sysfs(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (!policy)
Dave Jonesa07530b2008-03-05 14:22:25 -0500681 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800682
683 if (lock_policy_rwsem_write(policy->cpu) < 0)
Dave Jonesa07530b2008-03-05 14:22:25 -0500684 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800685
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530686 if (fattr->store)
687 ret = fattr->store(policy, buf, count);
688 else
689 ret = -EIO;
690
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800691 unlock_policy_rwsem_write(policy->cpu);
Dave Jonesa07530b2008-03-05 14:22:25 -0500692fail:
Stephen Boyda9144432012-07-20 18:14:38 +0000693 cpufreq_cpu_put_sysfs(policy);
Dave Jonesa07530b2008-03-05 14:22:25 -0500694no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return ret;
696}
697
Dave Jones905d77c2008-03-05 14:28:32 -0500698static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Dave Jones905d77c2008-03-05 14:28:32 -0500700 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200701 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 complete(&policy->kobj_unregister);
703}
704
Emese Revfy52cf25d2010-01-19 02:58:23 +0100705static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 .show = show,
707 .store = store,
708};
709
710static struct kobj_type ktype_cpufreq = {
711 .sysfs_ops = &sysfs_ops,
712 .default_attrs = default_attrs,
713 .release = cpufreq_sysfs_release,
714};
715
Dave Jones19d6f7e2009-07-08 17:35:39 -0400716/* symlink affected CPUs */
Alex Chiangcf3289d02009-11-17 20:27:08 -0700717static int cpufreq_add_dev_symlink(unsigned int cpu,
718 struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400719{
720 unsigned int j;
721 int ret = 0;
722
723 for_each_cpu(j, policy->cpus) {
724 struct cpufreq_policy *managed_policy;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800725 struct device *cpu_dev;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400726
727 if (j == cpu)
728 continue;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400729
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200730 pr_debug("CPU %u already managed, adding link\n", j);
Dave Jones19d6f7e2009-07-08 17:35:39 -0400731 managed_policy = cpufreq_cpu_get(cpu);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800732 cpu_dev = get_cpu_device(j);
733 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Dave Jones19d6f7e2009-07-08 17:35:39 -0400734 "cpufreq");
735 if (ret) {
736 cpufreq_cpu_put(managed_policy);
737 return ret;
738 }
739 }
740 return ret;
741}
742
Alex Chiangcf3289d02009-11-17 20:27:08 -0700743static int cpufreq_add_dev_interface(unsigned int cpu,
744 struct cpufreq_policy *policy,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800745 struct device *dev)
Dave Jones909a6942009-07-08 18:05:42 -0400746{
Dave Jonesecf7e462009-07-08 18:48:47 -0400747 struct cpufreq_policy new_policy;
Dave Jones909a6942009-07-08 18:05:42 -0400748 struct freq_attr **drv_attr;
749 unsigned long flags;
750 int ret = 0;
751 unsigned int j;
752
753 /* prepare interface data */
754 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800755 &dev->kobj, "cpufreq");
Dave Jones909a6942009-07-08 18:05:42 -0400756 if (ret)
757 return ret;
758
759 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200760 drv_attr = cpufreq_driver->attr;
Dave Jones909a6942009-07-08 18:05:42 -0400761 while ((drv_attr) && (*drv_attr)) {
762 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
763 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200764 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400765 drv_attr++;
766 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200767 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -0400768 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
769 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200770 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400771 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200772 if (cpufreq_driver->target) {
Dave Jones909a6942009-07-08 18:05:42 -0400773 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
774 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200775 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400776 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200777 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +0100778 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
779 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200780 goto err_out_kobj_put;
Thomas Renningere2f74f32009-11-19 12:31:01 +0100781 }
Dave Jones909a6942009-07-08 18:05:42 -0400782
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000783 write_lock_irqsave(&cpufreq_driver_lock, flags);
Dave Jones909a6942009-07-08 18:05:42 -0400784 for_each_cpu(j, policy->cpus) {
Dave Jones909a6942009-07-08 18:05:42 -0400785 per_cpu(cpufreq_cpu_data, j) = policy;
Tejun Heof1625062009-10-29 22:34:13 +0900786 per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
Dave Jones909a6942009-07-08 18:05:42 -0400787 }
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000788 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones909a6942009-07-08 18:05:42 -0400789
790 ret = cpufreq_add_dev_symlink(cpu, policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400791 if (ret)
792 goto err_out_kobj_put;
793
794 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
795 /* assure that the starting sequence is run in __cpufreq_set_policy */
796 policy->governor = NULL;
797
798 /* set default policy */
799 ret = __cpufreq_set_policy(policy, &new_policy);
800 policy->user_policy.policy = policy->policy;
801 policy->user_policy.governor = policy->governor;
802
803 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200804 pr_debug("setting policy failed\n");
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200805 if (cpufreq_driver->exit)
806 cpufreq_driver->exit(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400807 }
Dave Jones909a6942009-07-08 18:05:42 -0400808 return ret;
809
810err_out_kobj_put:
811 kobject_put(&policy->kobj);
812 wait_for_completion(&policy->kobj_unregister);
813 return ret;
814}
815
Viresh Kumarfcf80582013-01-29 14:39:08 +0000816#ifdef CONFIG_HOTPLUG_CPU
817static int cpufreq_add_policy_cpu(unsigned int cpu, unsigned int sibling,
818 struct device *dev)
819{
820 struct cpufreq_policy *policy;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200821 int ret = 0, has_target = !!cpufreq_driver->target;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000822 unsigned long flags;
823
824 policy = cpufreq_cpu_get(sibling);
825 WARN_ON(!policy);
826
Viresh Kumar820c6ca2013-04-22 00:48:03 +0200827 if (has_target)
828 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000829
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530830 lock_policy_rwsem_write(sibling);
831
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000832 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530833
Viresh Kumarfcf80582013-01-29 14:39:08 +0000834 cpumask_set_cpu(cpu, policy->cpus);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530835 per_cpu(cpufreq_policy_cpu, cpu) = policy->cpu;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000836 per_cpu(cpufreq_cpu_data, cpu) = policy;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000837 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000838
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530839 unlock_policy_rwsem_write(sibling);
840
Viresh Kumar820c6ca2013-04-22 00:48:03 +0200841 if (has_target) {
842 __cpufreq_governor(policy, CPUFREQ_GOV_START);
843 __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
844 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000845
Viresh Kumarfcf80582013-01-29 14:39:08 +0000846 ret = sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
847 if (ret) {
848 cpufreq_cpu_put(policy);
849 return ret;
850 }
851
852 return 0;
853}
854#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856/**
857 * cpufreq_add_dev - add a CPU device
858 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500859 * Adds the cpufreq interface for a CPU device.
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400860 *
861 * The Oracle says: try running cpufreq registration/unregistration concurrently
862 * with with cpu hotplugging and all hell will break loose. Tried to clean this
863 * mess up, but more thorough testing is needed. - Mathieu
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800865static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Viresh Kumarfcf80582013-01-29 14:39:08 +0000867 unsigned int j, cpu = dev->id;
Viresh Kumar65922462013-02-07 10:56:03 +0530868 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 unsigned long flags;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500871#ifdef CONFIG_HOTPLUG_CPU
Viresh Kumarfcf80582013-01-29 14:39:08 +0000872 struct cpufreq_governor *gov;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500873 int sibling;
874#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Ashok Rajc32b6b82005-10-30 14:59:54 -0800876 if (cpu_is_offline(cpu))
877 return 0;
878
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200879 pr_debug("adding CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881#ifdef CONFIG_SMP
882 /* check whether a different CPU already registered this
883 * CPU because it is in the same boat. */
884 policy = cpufreq_cpu_get(cpu);
885 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -0500886 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return 0;
888 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000889
890#ifdef CONFIG_HOTPLUG_CPU
891 /* Check if this cpu was hot-unplugged earlier and has siblings */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000892 read_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000893 for_each_online_cpu(sibling) {
894 struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530895 if (cp && cpumask_test_cpu(cpu, cp->related_cpus)) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000896 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000897 return cpufreq_add_policy_cpu(cpu, sibling, dev);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530898 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000899 }
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000900 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000901#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902#endif
903
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200904 if (!try_module_get(cpufreq_driver->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 ret = -EINVAL;
906 goto module_out;
907 }
908
Dave Jonese98df502005-10-20 15:17:43 -0700909 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
Dave Jones059019a2009-07-08 16:30:03 -0400910 if (!policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 goto nomem_out;
Dave Jones059019a2009-07-08 16:30:03 -0400912
913 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400914 goto err_free_policy;
Dave Jones059019a2009-07-08 16:30:03 -0400915
916 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400917 goto err_free_cpumask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 policy->cpu = cpu;
Viresh Kumar65922462013-02-07 10:56:03 +0530920 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Rusty Russell835481d2009-01-04 05:18:06 -0800921 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800923 /* Initially set CPU itself as the policy_cpu */
Tejun Heof1625062009-10-29 22:34:13 +0900924 per_cpu(cpufreq_policy_cpu, cpu) = cpu;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 init_completion(&policy->kobj_unregister);
David Howells65f27f32006-11-22 14:55:48 +0000927 INIT_WORK(&policy->update, handle_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 /* call driver. From then on the cpufreq must be able
930 * to accept all calls to ->verify and ->setpolicy for this CPU
931 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200932 ret = cpufreq_driver->init(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200934 pr_debug("initialization failed\n");
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530935 goto err_set_policy_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
Viresh Kumar643ae6e2013-01-12 05:14:38 +0000937
Viresh Kumarfcf80582013-01-29 14:39:08 +0000938 /* related cpus should atleast have policy->cpus */
939 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
940
Viresh Kumar643ae6e2013-01-12 05:14:38 +0000941 /*
942 * affected cpus must always be the one, which are online. We aren't
943 * managing offline cpus here.
944 */
945 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
946
Mike Chan187d9f42008-12-04 12:19:17 -0800947 policy->user_policy.min = policy->min;
948 policy->user_policy.max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Thomas Renningera1531ac2008-07-29 22:32:58 -0700950 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
951 CPUFREQ_START, policy);
952
Viresh Kumarfcf80582013-01-29 14:39:08 +0000953#ifdef CONFIG_HOTPLUG_CPU
954 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
955 if (gov) {
956 policy->governor = gov;
957 pr_debug("Restoring governor %s for cpu %d\n",
958 policy->governor->name, cpu);
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200959 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000960#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800962 ret = cpufreq_add_dev_interface(cpu, policy, dev);
Dave Jones19d6f7e2009-07-08 17:35:39 -0400963 if (ret)
964 goto err_out_unregister;
Dave Jones8ff69732006-03-05 03:37:23 -0500965
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -0400966 kobject_uevent(&policy->kobj, KOBJ_ADD);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200967 module_put(cpufreq_driver->owner);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200968 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -0500969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 return 0;
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972err_out_unregister:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000973 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rusty Russell835481d2009-01-04 05:18:06 -0800974 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -0700975 per_cpu(cpufreq_cpu_data, j) = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000976 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800978 kobject_put(&policy->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 wait_for_completion(&policy->kobj_unregister);
980
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530981err_set_policy_cpu:
982 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Xiaotian Fengcad70a62010-07-20 20:11:02 +0800983 free_cpumask_var(policy->related_cpus);
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400984err_free_cpumask:
985 free_cpumask_var(policy->cpus);
986err_free_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 kfree(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988nomem_out:
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200989 module_put(cpufreq_driver->owner);
Ashok Rajc32b6b82005-10-30 14:59:54 -0800990module_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 return ret;
992}
993
Viresh Kumarb8eed8a2013-01-14 13:23:03 +0000994static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
995{
996 int j;
997
998 policy->last_cpu = policy->cpu;
999 policy->cpu = cpu;
1000
Viresh Kumar3361b7b2013-02-04 11:38:51 +00001001 for_each_cpu(j, policy->cpus)
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001002 per_cpu(cpufreq_policy_cpu, j) = cpu;
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001003
1004#ifdef CONFIG_CPU_FREQ_TABLE
1005 cpufreq_frequency_table_update_policy_cpu(policy);
1006#endif
1007 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1008 CPUFREQ_UPDATE_POLICY_CPU, policy);
1009}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
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 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001018static int __cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001020 unsigned int cpu = dev->id, ret, cpus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 unsigned long flags;
1022 struct cpufreq_policy *data;
Amerigo Wang499bca92010-03-04 03:23:46 -05001023 struct kobject *kobj;
1024 struct completion *cmp;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001025 struct device *cpu_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001027 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001029 write_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 data = per_cpu(cpufreq_cpu_data, cpu);
Mike Travis7a6aedf2008-03-25 15:06:53 -07001032 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001034 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (!data) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001037 pr_debug("%s: No cpu_data found\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001041 if (cpufreq_driver->target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001043
Jacob Shin27ecddc2011-04-27 13:32:11 -05001044#ifdef CONFIG_HOTPLUG_CPU
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001045 if (!cpufreq_driver->setpolicy)
Dirk Brandewiefa69e332013-02-06 09:02:11 -08001046 strncpy(per_cpu(cpufreq_cpu_governor, cpu),
1047 data->governor->name, CPUFREQ_NAME_LEN);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001048#endif
1049
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301050 WARN_ON(lock_policy_rwsem_write(cpu));
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001051 cpus = cpumask_weight(data->cpus);
Viresh Kumare4969eb2013-04-11 08:04:53 +00001052
1053 if (cpus > 1)
1054 cpumask_clear_cpu(cpu, data->cpus);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301055 unlock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Viresh Kumar73bf0fc2013-02-05 22:21:14 +01001057 if (cpu != data->cpu) {
1058 sysfs_remove_link(&dev->kobj, "cpufreq");
1059 } else if (cpus > 1) {
Venki Pallipadiec282972007-03-26 12:03:19 -07001060 /* first sibling now owns the new sysfs dir */
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001061 cpu_dev = get_cpu_device(cpumask_first(data->cpus));
1062 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
1063 ret = kobject_move(&data->kobj, &cpu_dev->kobj);
1064 if (ret) {
1065 pr_err("%s: Failed to move kobj: %d", __func__, ret);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301066
1067 WARN_ON(lock_policy_rwsem_write(cpu));
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001068 cpumask_set_cpu(cpu, data->cpus);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301069
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001070 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301071 per_cpu(cpufreq_cpu_data, cpu) = data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001072 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301073
1074 unlock_policy_rwsem_write(cpu);
1075
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001076 ret = sysfs_create_link(&cpu_dev->kobj, &data->kobj,
1077 "cpufreq");
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001078 return -EINVAL;
1079 }
Venki Pallipadiec282972007-03-26 12:03:19 -07001080
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301081 WARN_ON(lock_policy_rwsem_write(cpu));
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001082 update_policy_cpu(data, cpu_dev->id);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301083 unlock_policy_rwsem_write(cpu);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001084 pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
1085 __func__, cpu_dev->id, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001086 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001087
Viresh Kumard96038e2013-04-30 14:32:18 +00001088 if ((cpus == 1) && (cpufreq_driver->target))
1089 __cpufreq_governor(data, CPUFREQ_GOV_POLICY_EXIT);
1090
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001091 pr_debug("%s: removing link, cpu: %d\n", __func__, cpu);
1092 cpufreq_cpu_put(data);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001093
1094 /* If cpu is last user of policy, free policy */
1095 if (cpus == 1) {
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301096 lock_policy_rwsem_read(cpu);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001097 kobj = &data->kobj;
1098 cmp = &data->kobj_unregister;
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301099 unlock_policy_rwsem_read(cpu);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001100 kobject_put(kobj);
1101
1102 /* we need to make sure that the underlying kobj is actually
1103 * not referenced anymore by anybody before we proceed with
1104 * unloading.
1105 */
1106 pr_debug("waiting for dropping of refcount\n");
1107 wait_for_completion(cmp);
1108 pr_debug("wait complete\n");
1109
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001110 if (cpufreq_driver->exit)
1111 cpufreq_driver->exit(data);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001112
1113 free_cpumask_var(data->related_cpus);
1114 free_cpumask_var(data->cpus);
1115 kfree(data);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001116 } else if (cpufreq_driver->target) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001117 __cpufreq_governor(data, CPUFREQ_GOV_START);
1118 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301121 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 return 0;
1123}
1124
1125
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001126static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001127{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001128 unsigned int cpu = dev->id;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001129 int retval;
1130
1131 if (cpu_is_offline(cpu))
1132 return 0;
1133
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001134 retval = __cpufreq_remove_dev(dev, sif);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001135 return retval;
1136}
1137
1138
David Howells65f27f32006-11-22 14:55:48 +00001139static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
David Howells65f27f32006-11-22 14:55:48 +00001141 struct cpufreq_policy *policy =
1142 container_of(work, struct cpufreq_policy, update);
1143 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001144 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 cpufreq_update_policy(cpu);
1146}
1147
1148/**
1149 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
1150 * @cpu: cpu number
1151 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1152 * @new_freq: CPU frequency the CPU actually runs at
1153 *
Dave Jones29464f22009-01-18 01:37:11 -05001154 * We adjust to current frequency first, and need to clean up later.
1155 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301157static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1158 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301160 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301162 unsigned long flags;
1163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001165 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 freqs.old = old_freq;
1169 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301170
1171 read_lock_irqsave(&cpufreq_driver_lock, flags);
1172 policy = per_cpu(cpufreq_cpu_data, cpu);
1173 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1174
1175 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1176 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178
1179
Dave Jones32ee8c32006-02-28 00:43:23 -05001180/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301181 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001182 * @cpu: CPU number
1183 *
1184 * This is the last known freq, without actually getting it from the driver.
1185 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1186 */
1187unsigned int cpufreq_quick_get(unsigned int cpu)
1188{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001189 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301190 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001191
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001192 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1193 return cpufreq_driver->get(cpu);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001194
1195 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001196 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301197 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001198 cpufreq_cpu_put(policy);
1199 }
1200
Dave Jones4d34a672008-02-07 16:33:49 -05001201 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001202}
1203EXPORT_SYMBOL(cpufreq_quick_get);
1204
Jesse Barnes3d737102011-06-28 10:59:12 -07001205/**
1206 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1207 * @cpu: CPU number
1208 *
1209 * Just return the max possible frequency for a given CPU.
1210 */
1211unsigned int cpufreq_quick_get_max(unsigned int cpu)
1212{
1213 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1214 unsigned int ret_freq = 0;
1215
1216 if (policy) {
1217 ret_freq = policy->max;
1218 cpufreq_cpu_put(policy);
1219 }
1220
1221 return ret_freq;
1222}
1223EXPORT_SYMBOL(cpufreq_quick_get_max);
1224
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001225
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001226static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001228 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301229 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001231 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001232 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001234 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301236 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001237 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301238 /* verify no discrepancy between actual and
1239 saved value exists */
1240 if (unlikely(ret_freq != policy->cur)) {
1241 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 schedule_work(&policy->update);
1243 }
1244 }
1245
Dave Jones4d34a672008-02-07 16:33:49 -05001246 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001247}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001249/**
1250 * cpufreq_get - get the current CPU frequency (in kHz)
1251 * @cpu: CPU number
1252 *
1253 * Get the CPU current (static) CPU frequency
1254 */
1255unsigned int cpufreq_get(unsigned int cpu)
1256{
1257 unsigned int ret_freq = 0;
1258 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1259
1260 if (!policy)
1261 goto out;
1262
1263 if (unlikely(lock_policy_rwsem_read(cpu)))
1264 goto out_policy;
1265
1266 ret_freq = __cpufreq_get(cpu);
1267
1268 unlock_policy_rwsem_read(cpu);
1269
1270out_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 cpufreq_cpu_put(policy);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001272out:
Dave Jones4d34a672008-02-07 16:33:49 -05001273 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}
1275EXPORT_SYMBOL(cpufreq_get);
1276
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001277static struct subsys_interface cpufreq_interface = {
1278 .name = "cpufreq",
1279 .subsys = &cpu_subsys,
1280 .add_dev = cpufreq_add_dev,
1281 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001282};
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001286 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1287 *
1288 * This function is only executed for the boot processor. The other CPUs
1289 * have been put offline by means of CPU hotplug.
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001290 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001291static int cpufreq_bp_suspend(void)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001292{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301293 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001294
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001295 int cpu = smp_processor_id();
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001296 struct cpufreq_policy *cpu_policy;
1297
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001298 pr_debug("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001299
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001300 /* If there's no policy for the boot CPU, we have nothing to do. */
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001301 cpu_policy = cpufreq_cpu_get(cpu);
1302 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001303 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001304
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001305 if (cpufreq_driver->suspend) {
1306 ret = cpufreq_driver->suspend(cpu_policy);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001307 if (ret)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001308 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1309 "step on CPU %u\n", cpu_policy->cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001310 }
1311
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001312 cpufreq_cpu_put(cpu_policy);
Dave Jonesc9060492008-02-07 16:32:18 -05001313 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001314}
1315
1316/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001317 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 *
1319 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001320 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1321 * restored. It will verify that the current freq is in sync with
1322 * what we believe it to be. This is a bit later than when it
1323 * should be, but nonethteless it's better than calling
1324 * cpufreq_driver->get() here which might re-enable interrupts...
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001325 *
1326 * This function is only executed for the boot CPU. The other CPUs have not
1327 * been turned on yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001329static void cpufreq_bp_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301331 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001332
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001333 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 struct cpufreq_policy *cpu_policy;
1335
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001336 pr_debug("resuming cpu %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001338 /* If there's no policy for the boot CPU, we have nothing to do. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 cpu_policy = cpufreq_cpu_get(cpu);
1340 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001341 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001343 if (cpufreq_driver->resume) {
1344 ret = cpufreq_driver->resume(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 if (ret) {
1346 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1347 "step on CPU %u\n", cpu_policy->cpu);
Dave Jonesc9060492008-02-07 16:32:18 -05001348 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
1350 }
1351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 schedule_work(&cpu_policy->update);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001353
Dave Jonesc9060492008-02-07 16:32:18 -05001354fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356}
1357
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001358static struct syscore_ops cpufreq_syscore_ops = {
1359 .suspend = cpufreq_bp_suspend,
1360 .resume = cpufreq_bp_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361};
1362
Borislav Petkov9d950462013-01-20 10:24:28 +00001363/**
1364 * cpufreq_get_current_driver - return current driver's name
1365 *
1366 * Return the name string of the currently loaded cpufreq driver
1367 * or NULL, if none.
1368 */
1369const char *cpufreq_get_current_driver(void)
1370{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001371 if (cpufreq_driver)
1372 return cpufreq_driver->name;
1373
1374 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001375}
1376EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378/*********************************************************************
1379 * NOTIFIER LISTS INTERFACE *
1380 *********************************************************************/
1381
1382/**
1383 * cpufreq_register_notifier - register a driver with cpufreq
1384 * @nb: notifier function to register
1385 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1386 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001387 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 * are notified about clock rate changes (once before and once after
1389 * the transition), or a list of drivers that are notified about
1390 * changes in cpufreq policy.
1391 *
1392 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001393 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 */
1395int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1396{
1397 int ret;
1398
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001399 if (cpufreq_disabled())
1400 return -EINVAL;
1401
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001402 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 switch (list) {
1405 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001406 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001407 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 break;
1409 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001410 ret = blocking_notifier_chain_register(
1411 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 break;
1413 default:
1414 ret = -EINVAL;
1415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 return ret;
1418}
1419EXPORT_SYMBOL(cpufreq_register_notifier);
1420
1421
1422/**
1423 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1424 * @nb: notifier block to be unregistered
1425 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1426 *
1427 * Remove a driver from the CPU frequency notifier list.
1428 *
1429 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001430 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 */
1432int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1433{
1434 int ret;
1435
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001436 if (cpufreq_disabled())
1437 return -EINVAL;
1438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 switch (list) {
1440 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001441 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001442 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 break;
1444 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001445 ret = blocking_notifier_chain_unregister(
1446 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 break;
1448 default:
1449 ret = -EINVAL;
1450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 return ret;
1453}
1454EXPORT_SYMBOL(cpufreq_unregister_notifier);
1455
1456
1457/*********************************************************************
1458 * GOVERNORS *
1459 *********************************************************************/
1460
1461
1462int __cpufreq_driver_target(struct cpufreq_policy *policy,
1463 unsigned int target_freq,
1464 unsigned int relation)
1465{
1466 int retval = -EINVAL;
Viresh Kumar72499242012-10-31 01:28:21 +01001467 unsigned int old_target_freq = target_freq;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001468
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001469 if (cpufreq_disabled())
1470 return -ENODEV;
1471
Viresh Kumar72499242012-10-31 01:28:21 +01001472 /* Make sure that target_freq is within supported range */
1473 if (target_freq > policy->max)
1474 target_freq = policy->max;
1475 if (target_freq < policy->min)
1476 target_freq = policy->min;
1477
1478 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1479 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001480
1481 if (target_freq == policy->cur)
1482 return 0;
1483
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001484 if (cpufreq_driver->target)
1485 retval = cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 return retval;
1488}
1489EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491int cpufreq_driver_target(struct cpufreq_policy *policy,
1492 unsigned int target_freq,
1493 unsigned int relation)
1494{
Julia Lawallf1829e42008-07-25 22:44:53 +02001495 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497 policy = cpufreq_cpu_get(policy->cpu);
1498 if (!policy)
Julia Lawallf1829e42008-07-25 22:44:53 +02001499 goto no_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001501 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
Julia Lawallf1829e42008-07-25 22:44:53 +02001502 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504 ret = __cpufreq_driver_target(policy, target_freq, relation);
1505
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001506 unlock_policy_rwsem_write(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Julia Lawallf1829e42008-07-25 22:44:53 +02001508fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 cpufreq_cpu_put(policy);
Julia Lawallf1829e42008-07-25 22:44:53 +02001510no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 return ret;
1512}
1513EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1514
venkatesh.pallipadi@intel.combf0b90e2008-08-04 11:59:07 -07001515int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu)
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001516{
1517 int ret = 0;
1518
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001519 if (cpufreq_disabled())
1520 return ret;
1521
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001522 if (!cpufreq_driver->getavg)
Viresh Kumar0676f7f2012-10-24 23:39:48 +02001523 return 0;
1524
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001525 policy = cpufreq_cpu_get(policy->cpu);
1526 if (!policy)
1527 return -EINVAL;
1528
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001529 ret = cpufreq_driver->getavg(policy, cpu);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001530
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001531 cpufreq_cpu_put(policy);
1532 return ret;
1533}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001534EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001535
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001536/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001537 * when "event" is CPUFREQ_GOV_LIMITS
1538 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301540static int __cpufreq_governor(struct cpufreq_policy *policy,
1541 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
Dave Jonescc993ca2005-07-28 09:43:56 -07001543 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001544
1545 /* Only must be defined when default governor is known to have latency
1546 restrictions, like e.g. conservative or ondemand.
1547 That this is the case is already ensured in Kconfig
1548 */
1549#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1550 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1551#else
1552 struct cpufreq_governor *gov = NULL;
1553#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001554
1555 if (policy->governor->max_transition_latency &&
1556 policy->cpuinfo.transition_latency >
1557 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07001558 if (!gov)
1559 return -EINVAL;
1560 else {
1561 printk(KERN_WARNING "%s governor failed, too long"
1562 " transition latency of HW, fallback"
1563 " to %s governor\n",
1564 policy->governor->name,
1565 gov->name);
1566 policy->governor = gov;
1567 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570 if (!try_module_get(policy->governor->owner))
1571 return -EINVAL;
1572
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001573 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301574 policy->cpu, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 ret = policy->governor->governor(policy, event);
1576
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001577 if (!ret) {
1578 if (event == CPUFREQ_GOV_POLICY_INIT)
1579 policy->governor->initialized++;
1580 else if (event == CPUFREQ_GOV_POLICY_EXIT)
1581 policy->governor->initialized--;
1582 }
Viresh Kumarb3940582013-02-01 05:42:58 +00001583
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301584 /* we keep one module reference alive for
1585 each CPU governed by this CPU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 if ((event != CPUFREQ_GOV_START) || ret)
1587 module_put(policy->governor->owner);
1588 if ((event == CPUFREQ_GOV_STOP) && !ret)
1589 module_put(policy->governor->owner);
1590
1591 return ret;
1592}
1593
1594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595int cpufreq_register_governor(struct cpufreq_governor *governor)
1596{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001597 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599 if (!governor)
1600 return -EINVAL;
1601
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001602 if (cpufreq_disabled())
1603 return -ENODEV;
1604
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001605 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001606
Viresh Kumarb3940582013-02-01 05:42:58 +00001607 governor->initialized = 0;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001608 err = -EBUSY;
1609 if (__find_governor(governor->name) == NULL) {
1610 err = 0;
1611 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Dave Jones32ee8c32006-02-28 00:43:23 -05001614 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001615 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616}
1617EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1618
1619
1620void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1621{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001622#ifdef CONFIG_HOTPLUG_CPU
1623 int cpu;
1624#endif
1625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 if (!governor)
1627 return;
1628
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001629 if (cpufreq_disabled())
1630 return;
1631
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001632#ifdef CONFIG_HOTPLUG_CPU
1633 for_each_present_cpu(cpu) {
1634 if (cpu_online(cpu))
1635 continue;
1636 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1637 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1638 }
1639#endif
1640
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001641 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001643 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 return;
1645}
1646EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1647
1648
1649
1650/*********************************************************************
1651 * POLICY INTERFACE *
1652 *********************************************************************/
1653
1654/**
1655 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05001656 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1657 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 *
1659 * Reads the current cpufreq policy.
1660 */
1661int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1662{
1663 struct cpufreq_policy *cpu_policy;
1664 if (!policy)
1665 return -EINVAL;
1666
1667 cpu_policy = cpufreq_cpu_get(cpu);
1668 if (!cpu_policy)
1669 return -EINVAL;
1670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
1673 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 return 0;
1675}
1676EXPORT_SYMBOL(cpufreq_get_policy);
1677
1678
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001679/*
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301680 * data : current policy.
1681 * policy : policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001682 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301683static int __cpufreq_set_policy(struct cpufreq_policy *data,
1684 struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001686 int ret = 0, failed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001688 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 policy->min, policy->max);
1690
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301691 memcpy(&policy->cpuinfo, &data->cpuinfo,
1692 sizeof(struct cpufreq_cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Yi Yang53391fa2008-01-30 13:33:34 +01001694 if (policy->min > data->max || policy->max < data->min) {
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02001695 ret = -EINVAL;
1696 goto error_out;
1697 }
1698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 /* verify the cpu speed can be set within this limit */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001700 ret = cpufreq_driver->verify(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 if (ret)
1702 goto error_out;
1703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001705 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1706 CPUFREQ_ADJUST, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
1708 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001709 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1710 CPUFREQ_INCOMPATIBLE, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712 /* verify the cpu speed can be set within this limit,
1713 which might be different to the first one */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001714 ret = cpufreq_driver->verify(policy);
Alan Sterne041c682006-03-27 01:16:30 -08001715 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
1718 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001719 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1720 CPUFREQ_NOTIFY, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Dave Jones7d5e3502006-02-02 17:03:42 -05001722 data->min = policy->min;
1723 data->max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001725 pr_debug("new min and max freqs are %u - %u kHz\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301726 data->min, data->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001728 if (cpufreq_driver->setpolicy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 data->policy = policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001730 pr_debug("setting range\n");
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001731 ret = cpufreq_driver->setpolicy(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 } else {
1733 if (policy->governor != data->governor) {
1734 /* save old, working values */
1735 struct cpufreq_governor *old_gov = data->governor;
1736
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001737 pr_debug("governor switch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 /* end old governor */
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001740 if (data->governor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Viresh Kumar955ef482013-05-16 05:09:58 +00001742 unlock_policy_rwsem_write(policy->cpu);
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001743 __cpufreq_governor(data,
1744 CPUFREQ_GOV_POLICY_EXIT);
Viresh Kumar955ef482013-05-16 05:09:58 +00001745 lock_policy_rwsem_write(policy->cpu);
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
1748 /* start new governor */
1749 data->governor = policy->governor;
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001750 if (!__cpufreq_governor(data, CPUFREQ_GOV_POLICY_INIT)) {
Viresh Kumar955ef482013-05-16 05:09:58 +00001751 if (!__cpufreq_governor(data, CPUFREQ_GOV_START)) {
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001752 failed = 0;
Viresh Kumar955ef482013-05-16 05:09:58 +00001753 } else {
1754 unlock_policy_rwsem_write(policy->cpu);
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001755 __cpufreq_governor(data,
1756 CPUFREQ_GOV_POLICY_EXIT);
Viresh Kumar955ef482013-05-16 05:09:58 +00001757 lock_policy_rwsem_write(policy->cpu);
1758 }
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001759 }
1760
1761 if (failed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 /* new governor failed, so re-start old one */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001763 pr_debug("starting governor %s failed\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301764 data->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 if (old_gov) {
1766 data->governor = old_gov;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301767 __cpufreq_governor(data,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001768 CPUFREQ_GOV_POLICY_INIT);
1769 __cpufreq_governor(data,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301770 CPUFREQ_GOV_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 }
1772 ret = -EINVAL;
1773 goto error_out;
1774 }
1775 /* might be a policy change, too, so fall through */
1776 }
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001777 pr_debug("governor: change or update limits\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1779 }
1780
Dave Jones7d5e3502006-02-02 17:03:42 -05001781error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 return ret;
1783}
1784
1785/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1787 * @cpu: CPU which shall be re-evaluated
1788 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001789 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 * at different times.
1791 */
1792int cpufreq_update_policy(unsigned int cpu)
1793{
1794 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1795 struct cpufreq_policy policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02001796 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Julia Lawallf1829e42008-07-25 22:44:53 +02001798 if (!data) {
1799 ret = -ENODEV;
1800 goto no_policy;
1801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Julia Lawallf1829e42008-07-25 22:44:53 +02001803 if (unlikely(lock_policy_rwsem_write(cpu))) {
1804 ret = -EINVAL;
1805 goto fail;
1806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001808 pr_debug("updating policy for CPU %u\n", cpu);
Dave Jones7d5e3502006-02-02 17:03:42 -05001809 memcpy(&policy, data, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 policy.min = data->user_policy.min;
1811 policy.max = data->user_policy.max;
1812 policy.policy = data->user_policy.policy;
1813 policy.governor = data->user_policy.governor;
1814
Thomas Renninger0961dd02006-01-26 18:46:33 +01001815 /* BIOS might change freq behind our back
1816 -> ask driver for current freq and notify governors about a change */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001817 if (cpufreq_driver->get) {
1818 policy.cur = cpufreq_driver->get(cpu);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001819 if (!data->cur) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001820 pr_debug("Driver did not initialize current freq");
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001821 data->cur = policy.cur;
1822 } else {
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001823 if (data->cur != policy.cur && cpufreq_driver->target)
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301824 cpufreq_out_of_sync(cpu, data->cur,
1825 policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001826 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01001827 }
1828
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 ret = __cpufreq_set_policy(data, &policy);
1830
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001831 unlock_policy_rwsem_write(cpu);
1832
Julia Lawallf1829e42008-07-25 22:44:53 +02001833fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 cpufreq_cpu_put(data);
Julia Lawallf1829e42008-07-25 22:44:53 +02001835no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 return ret;
1837}
1838EXPORT_SYMBOL(cpufreq_update_policy);
1839
Satyam Sharmadd184a02007-10-02 13:28:14 -07001840static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08001841 unsigned long action, void *hcpu)
1842{
1843 unsigned int cpu = (unsigned long)hcpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001844 struct device *dev;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001845
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001846 dev = get_cpu_device(cpu);
1847 if (dev) {
Ashok Rajc32b6b82005-10-30 14:59:54 -08001848 switch (action) {
1849 case CPU_ONLINE:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001850 cpufreq_add_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001851 break;
1852 case CPU_DOWN_PREPARE:
Srivatsa S. Bhata66b2e52013-05-15 21:47:17 +02001853 case CPU_UP_CANCELED_FROZEN:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001854 __cpufreq_remove_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001855 break;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001856 case CPU_DOWN_FAILED:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001857 cpufreq_add_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001858 break;
1859 }
1860 }
1861 return NOTIFY_OK;
1862}
1863
Neal Buckendahl9c36f742010-06-22 22:02:44 -05001864static struct notifier_block __refdata cpufreq_cpu_notifier = {
Ashok Rajc32b6b82005-10-30 14:59:54 -08001865 .notifier_call = cpufreq_cpu_callback,
1866};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868/*********************************************************************
1869 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1870 *********************************************************************/
1871
1872/**
1873 * cpufreq_register_driver - register a CPU Frequency driver
1874 * @driver_data: A struct cpufreq_driver containing the values#
1875 * submitted by the CPU Frequency driver.
1876 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001877 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05001879 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 *
1881 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001882int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
1884 unsigned long flags;
1885 int ret;
1886
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001887 if (cpufreq_disabled())
1888 return -ENODEV;
1889
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 if (!driver_data || !driver_data->verify || !driver_data->init ||
1891 ((!driver_data->setpolicy) && (!driver_data->target)))
1892 return -EINVAL;
1893
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001894 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
1896 if (driver_data->setpolicy)
1897 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1898
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001899 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001900 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001901 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 return -EBUSY;
1903 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001904 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001905 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001907 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001908 if (ret)
1909 goto err_null_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001911 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 int i;
1913 ret = -ENODEV;
1914
1915 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07001916 for (i = 0; i < nr_cpu_ids; i++)
1917 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07001919 break;
1920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
1922 /* if all ->init() calls failed, unregister */
1923 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001924 pr_debug("no CPU initialized for driver %s\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301925 driver_data->name);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001926 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 }
1928 }
1929
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001930 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001931 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001933 return 0;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001934err_if_unreg:
1935 subsys_interface_unregister(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001936err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001937 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001938 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001939 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05001940 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941}
1942EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1943
1944
1945/**
1946 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1947 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001948 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 * the right to do so, i.e. if you have succeeded in initialising before!
1950 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1951 * currently not initialised.
1952 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001953int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954{
1955 unsigned long flags;
1956
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001957 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001960 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001962 subsys_interface_unregister(&cpufreq_interface);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001963 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001965 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001966 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001967 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
1969 return 0;
1970}
1971EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001972
1973static int __init cpufreq_core_init(void)
1974{
1975 int cpu;
1976
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001977 if (cpufreq_disabled())
1978 return -ENODEV;
1979
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001980 for_each_possible_cpu(cpu) {
Tejun Heof1625062009-10-29 22:34:13 +09001981 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001982 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
1983 }
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02001984
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001985 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02001986 BUG_ON(!cpufreq_global_kobject);
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001987 register_syscore_ops(&cpufreq_syscore_ops);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02001988
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001989 return 0;
1990}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001991core_initcall(cpufreq_core_init);