blob: 66f6cf5064ece85d201731b58757b594206b2bb7 [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);
Xiaoguang Chenba17ca42013-06-19 15:00:07 +080049static DEFINE_MUTEX(cpufreq_governor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080051/*
52 * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
53 * all cpufreq/hotplug/workqueue/etc related lock issues.
54 *
55 * The rules for this semaphore:
56 * - Any routine that wants to read from the policy structure will
57 * do a down_read on this semaphore.
58 * - Any routine that will write to the policy structure and/or may take away
59 * the policy altogether (eg. CPU hotplug), will hold this lock in write
60 * mode before doing so.
61 *
62 * Additional rules:
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080063 * - Governor routines that can be called in cpufreq hotplug path should not
64 * take this sem as top level hotplug notifier handler takes this.
Mathieu Desnoyers395913d2009-06-08 13:17:31 -040065 * - Lock should not be held across
66 * __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080067 */
Tejun Heof1625062009-10-29 22:34:13 +090068static DEFINE_PER_CPU(int, cpufreq_policy_cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080069static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
70
71#define lock_policy_rwsem(mode, cpu) \
Viresh Kumarfa1d8af2013-02-07 15:38:42 +053072static int lock_policy_rwsem_##mode(int cpu) \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080073{ \
Tejun Heof1625062009-10-29 22:34:13 +090074 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080075 BUG_ON(policy_cpu == -1); \
76 down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080077 \
78 return 0; \
79}
80
81lock_policy_rwsem(read, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080082lock_policy_rwsem(write, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080083
Viresh Kumarfa1d8af2013-02-07 15:38:42 +053084#define unlock_policy_rwsem(mode, cpu) \
85static void unlock_policy_rwsem_##mode(int cpu) \
86{ \
87 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
88 BUG_ON(policy_cpu == -1); \
89 up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080090}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080091
Viresh Kumarfa1d8af2013-02-07 15:38:42 +053092unlock_policy_rwsem(read, cpu);
93unlock_policy_rwsem(write, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/* internal prototypes */
Dave Jones29464f22009-01-18 01:37:11 -050096static int __cpufreq_governor(struct cpufreq_policy *policy,
97 unsigned int event);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080098static unsigned int __cpufreq_get(unsigned int cpu);
David Howells65f27f32006-11-22 14:55:48 +000099static void handle_update(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101/**
Dave Jones32ee8c32006-02-28 00:43:23 -0500102 * Two notifier lists: the "policy" list is involved in the
103 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 * "transition" list for kernel code that needs to handle
105 * changes to devices when the CPU clock speed changes.
106 * The mutex locks both lists.
107 */
Alan Sterne041c682006-03-27 01:16:30 -0800108static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700109static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200111static bool init_cpufreq_transition_notifier_list_called;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700112static int __init init_cpufreq_transition_notifier_list(void)
113{
114 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200115 init_cpufreq_transition_notifier_list_called = true;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700116 return 0;
117}
Linus Torvaldsb3438f82006-11-20 11:47:18 -0800118pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400120static int off __read_mostly;
Viresh Kumarda584452012-10-26 00:51:32 +0200121static int cpufreq_disabled(void)
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400122{
123 return off;
124}
125void disable_cpufreq(void)
126{
127 off = 1;
128}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129static LIST_HEAD(cpufreq_governor_list);
Dave Jones29464f22009-01-18 01:37:11 -0500130static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000132bool have_governor_per_policy(void)
133{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200134 return cpufreq_driver->have_governor_per_policy;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000135}
136
Stephen Boyda9144432012-07-20 18:14:38 +0000137static struct cpufreq_policy *__cpufreq_cpu_get(unsigned int cpu, bool sysfs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 struct cpufreq_policy *data;
140 unsigned long flags;
141
Mike Travis7a6aedf2008-03-25 15:06:53 -0700142 if (cpu >= nr_cpu_ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 goto err_out;
144
145 /* get the cpufreq driver */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000146 read_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200148 if (!cpufreq_driver)
149 goto err_out_unlock;
150
151 if (!try_module_get(cpufreq_driver->owner))
152 goto err_out_unlock;
153
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 /* get the CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -0700156 data = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 if (!data)
159 goto err_out_put_module;
160
Stephen Boyda9144432012-07-20 18:14:38 +0000161 if (!sysfs && !kobject_get(&data->kobj))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 goto err_out_put_module;
163
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000164 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return data;
166
Dave Jones7d5e3502006-02-02 17:03:42 -0500167err_out_put_module:
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200168 module_put(cpufreq_driver->owner);
Nathan Zimmer58000432013-04-04 14:53:25 +0000169err_out_unlock:
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200170 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones7d5e3502006-02-02 17:03:42 -0500171err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return NULL;
173}
Stephen Boyda9144432012-07-20 18:14:38 +0000174
175struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
176{
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000177 if (cpufreq_disabled())
178 return NULL;
179
Stephen Boyda9144432012-07-20 18:14:38 +0000180 return __cpufreq_cpu_get(cpu, false);
181}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
183
Stephen Boyda9144432012-07-20 18:14:38 +0000184static struct cpufreq_policy *cpufreq_cpu_get_sysfs(unsigned int cpu)
185{
186 return __cpufreq_cpu_get(cpu, true);
187}
188
189static void __cpufreq_cpu_put(struct cpufreq_policy *data, bool sysfs)
190{
191 if (!sysfs)
192 kobject_put(&data->kobj);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200193 module_put(cpufreq_driver->owner);
Stephen Boyda9144432012-07-20 18:14:38 +0000194}
Dave Jones7d5e3502006-02-02 17:03:42 -0500195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196void cpufreq_cpu_put(struct cpufreq_policy *data)
197{
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000198 if (cpufreq_disabled())
199 return;
200
Stephen Boyda9144432012-07-20 18:14:38 +0000201 __cpufreq_cpu_put(data, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
204
Stephen Boyda9144432012-07-20 18:14:38 +0000205static void cpufreq_cpu_put_sysfs(struct cpufreq_policy *data)
206{
207 __cpufreq_cpu_put(data, true);
208}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
212 *********************************************************************/
213
214/**
215 * adjust_jiffies - adjust the system "loops_per_jiffy"
216 *
217 * This function alters the system "loops_per_jiffy" for the clock
218 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500219 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 * per-CPU loops_per_jiffy value wherever possible.
221 */
222#ifndef CONFIG_SMP
223static unsigned long l_p_j_ref;
224static unsigned int l_p_j_ref_freq;
225
Arjan van de Ven858119e2006-01-14 13:20:43 -0800226static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 if (ci->flags & CPUFREQ_CONST_LOOPS)
229 return;
230
231 if (!l_p_j_ref_freq) {
232 l_p_j_ref = loops_per_jiffy;
233 l_p_j_ref_freq = ci->old;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200234 pr_debug("saving %lu as reference value for loops_per_jiffy; "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530235 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
Afzal Mohammedd08de0c12012-01-04 10:52:46 +0530237 if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700238 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530239 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
240 ci->new);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200241 pr_debug("scaling loops_per_jiffy to %lu "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530242 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244}
245#else
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530246static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
247{
248 return;
249}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250#endif
251
252
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530253void __cpufreq_notify_transition(struct cpufreq_policy *policy,
254 struct cpufreq_freqs *freqs, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 BUG_ON(irqs_disabled());
257
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000258 if (cpufreq_disabled())
259 return;
260
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200261 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200262 pr_debug("notification %u of frequency transition to %u kHz\n",
Dave Jonese4472cb2006-01-31 15:53:55 -0800263 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500268 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800269 * which is not equal to what the cpufreq core thinks is
270 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200272 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800273 if ((policy) && (policy->cpu == freqs->cpu) &&
274 (policy->cur) && (policy->cur != freqs->old)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200275 pr_debug("Warning: CPU frequency is"
Dave Jonese4472cb2006-01-31 15:53:55 -0800276 " %u, cpufreq assumed %u kHz.\n",
277 freqs->old, policy->cur);
278 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
280 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700281 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800282 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
284 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 case CPUFREQ_POSTCHANGE:
287 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200288 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
Thomas Renninger6f4f2722010-04-20 13:17:36 +0200289 (unsigned long)freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100290 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700291 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800292 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800293 if (likely(policy) && likely(policy->cpu == freqs->cpu))
294 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 break;
296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530298/**
299 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
300 * on frequency transition.
301 *
302 * This function calls the transition notifiers and the "adjust_jiffies"
303 * function. It is called twice on all CPU frequency changes that have
304 * external effects.
305 */
306void cpufreq_notify_transition(struct cpufreq_policy *policy,
307 struct cpufreq_freqs *freqs, unsigned int state)
308{
309 for_each_cpu(freqs->cpu, policy->cpus)
310 __cpufreq_notify_transition(policy, freqs, state);
311}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
313
314
315
316/*********************************************************************
317 * SYSFS INTERFACE *
318 *********************************************************************/
319
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700320static struct cpufreq_governor *__find_governor(const char *str_governor)
321{
322 struct cpufreq_governor *t;
323
324 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
Dave Jones29464f22009-01-18 01:37:11 -0500325 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700326 return t;
327
328 return NULL;
329}
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331/**
332 * cpufreq_parse_governor - parse a governor string
333 */
Dave Jones905d77c2008-03-05 14:28:32 -0500334static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 struct cpufreq_governor **governor)
336{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700337 int err = -EINVAL;
338
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200339 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700340 goto out;
341
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200342 if (cpufreq_driver->setpolicy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
344 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700345 err = 0;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530346 } else if (!strnicmp(str_governor, "powersave",
347 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700349 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200351 } else if (cpufreq_driver->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700353
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800354 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700355
356 t = __find_governor(str_governor);
357
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700358 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700359 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700360
Kees Cook1a8e1462011-05-04 08:38:56 -0700361 mutex_unlock(&cpufreq_governor_mutex);
362 ret = request_module("cpufreq_%s", str_governor);
363 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700364
Kees Cook1a8e1462011-05-04 08:38:56 -0700365 if (ret == 0)
366 t = __find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700367 }
368
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700369 if (t != NULL) {
370 *governor = t;
371 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700373
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800374 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
Dave Jones29464f22009-01-18 01:37:11 -0500376out:
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700377 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530382 * cpufreq_per_cpu_attr_read() / show_##file_name() -
383 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 *
385 * Write out information from cpufreq_driver->policy[cpu]; object must be
386 * "unsigned int".
387 */
388
Dave Jones32ee8c32006-02-28 00:43:23 -0500389#define show_one(file_name, object) \
390static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500391(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500392{ \
Dave Jones29464f22009-01-18 01:37:11 -0500393 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
396show_one(cpuinfo_min_freq, cpuinfo.min_freq);
397show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100398show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399show_one(scaling_min_freq, min);
400show_one(scaling_max_freq, max);
401show_one(scaling_cur_freq, cur);
402
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530403static int __cpufreq_set_policy(struct cpufreq_policy *data,
404 struct cpufreq_policy *policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406/**
407 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
408 */
409#define store_one(file_name, object) \
410static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500411(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{ \
Jingoo Hanf55c9c22012-10-31 05:49:13 +0000413 unsigned int ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 struct cpufreq_policy new_policy; \
415 \
416 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
417 if (ret) \
418 return -EINVAL; \
419 \
Dave Jones29464f22009-01-18 01:37:11 -0500420 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (ret != 1) \
422 return -EINVAL; \
423 \
Thomas Renninger7970e082006-04-13 15:14:04 +0200424 ret = __cpufreq_set_policy(policy, &new_policy); \
425 policy->user_policy.object = policy->object; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 \
427 return ret ? ret : count; \
428}
429
Dave Jones29464f22009-01-18 01:37:11 -0500430store_one(scaling_min_freq, min);
431store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433/**
434 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
435 */
Dave Jones905d77c2008-03-05 14:28:32 -0500436static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
437 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800439 unsigned int cur_freq = __cpufreq_get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (!cur_freq)
441 return sprintf(buf, "<unknown>");
442 return sprintf(buf, "%u\n", cur_freq);
443}
444
445
446/**
447 * show_scaling_governor - show the current policy for the specified CPU
448 */
Dave Jones905d77c2008-03-05 14:28:32 -0500449static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Dave Jones29464f22009-01-18 01:37:11 -0500451 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return sprintf(buf, "powersave\n");
453 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
454 return sprintf(buf, "performance\n");
455 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200456 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500457 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return -EINVAL;
459}
460
461
462/**
463 * store_scaling_governor - store policy for the specified CPU
464 */
Dave Jones905d77c2008-03-05 14:28:32 -0500465static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
466 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Jingoo Hanf55c9c22012-10-31 05:49:13 +0000468 unsigned int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 char str_governor[16];
470 struct cpufreq_policy new_policy;
471
472 ret = cpufreq_get_policy(&new_policy, policy->cpu);
473 if (ret)
474 return ret;
475
Dave Jones29464f22009-01-18 01:37:11 -0500476 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (ret != 1)
478 return -EINVAL;
479
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530480 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
481 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return -EINVAL;
483
Thomas Renninger7970e082006-04-13 15:14:04 +0200484 /* Do not use cpufreq_set_policy here or the user_policy.max
485 will be wrongly overridden */
Thomas Renninger7970e082006-04-13 15:14:04 +0200486 ret = __cpufreq_set_policy(policy, &new_policy);
487
488 policy->user_policy.policy = policy->policy;
489 policy->user_policy.governor = policy->governor;
Thomas Renninger7970e082006-04-13 15:14:04 +0200490
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530491 if (ret)
492 return ret;
493 else
494 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
497/**
498 * show_scaling_driver - show the cpufreq driver currently loaded
499 */
Dave Jones905d77c2008-03-05 14:28:32 -0500500static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200502 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
505/**
506 * show_scaling_available_governors - show the available CPUfreq governors
507 */
Dave Jones905d77c2008-03-05 14:28:32 -0500508static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
509 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 ssize_t i = 0;
512 struct cpufreq_governor *t;
513
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200514 if (!cpufreq_driver->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 i += sprintf(buf, "performance powersave");
516 goto out;
517 }
518
519 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
Dave Jones29464f22009-01-18 01:37:11 -0500520 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
521 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200523 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500525out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 i += sprintf(&buf[i], "\n");
527 return i;
528}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700529
Rusty Russell835481d2009-01-04 05:18:06 -0800530static ssize_t show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
532 ssize_t i = 0;
533 unsigned int cpu;
534
Rusty Russell835481d2009-01-04 05:18:06 -0800535 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (i)
537 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
538 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
539 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500540 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
542 i += sprintf(&buf[i], "\n");
543 return i;
544}
545
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700546/**
547 * show_related_cpus - show the CPUs affected by each transition even if
548 * hw coordination is in use
549 */
550static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
551{
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700552 return show_cpus(policy->related_cpus, buf);
553}
554
555/**
556 * show_affected_cpus - show the CPUs affected by each transition
557 */
558static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
559{
560 return show_cpus(policy->cpus, buf);
561}
562
Venki Pallipadi9e769882007-10-26 10:18:21 -0700563static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500564 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700565{
566 unsigned int freq = 0;
567 unsigned int ret;
568
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700569 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700570 return -EINVAL;
571
572 ret = sscanf(buf, "%u", &freq);
573 if (ret != 1)
574 return -EINVAL;
575
576 policy->governor->store_setspeed(policy, freq);
577
578 return count;
579}
580
581static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
582{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700583 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700584 return sprintf(buf, "<unsupported>\n");
585
586 return policy->governor->show_setspeed(policy, buf);
587}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Thomas Renningere2f74f32009-11-19 12:31:01 +0100589/**
viresh kumar8bf1ac722012-10-23 01:23:33 +0200590 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100591 */
592static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
593{
594 unsigned int limit;
595 int ret;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200596 if (cpufreq_driver->bios_limit) {
597 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
Thomas Renningere2f74f32009-11-19 12:31:01 +0100598 if (!ret)
599 return sprintf(buf, "%u\n", limit);
600 }
601 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
602}
603
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200604cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
605cpufreq_freq_attr_ro(cpuinfo_min_freq);
606cpufreq_freq_attr_ro(cpuinfo_max_freq);
607cpufreq_freq_attr_ro(cpuinfo_transition_latency);
608cpufreq_freq_attr_ro(scaling_available_governors);
609cpufreq_freq_attr_ro(scaling_driver);
610cpufreq_freq_attr_ro(scaling_cur_freq);
611cpufreq_freq_attr_ro(bios_limit);
612cpufreq_freq_attr_ro(related_cpus);
613cpufreq_freq_attr_ro(affected_cpus);
614cpufreq_freq_attr_rw(scaling_min_freq);
615cpufreq_freq_attr_rw(scaling_max_freq);
616cpufreq_freq_attr_rw(scaling_governor);
617cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Dave Jones905d77c2008-03-05 14:28:32 -0500619static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 &cpuinfo_min_freq.attr,
621 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100622 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 &scaling_min_freq.attr,
624 &scaling_max_freq.attr,
625 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700626 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 &scaling_governor.attr,
628 &scaling_driver.attr,
629 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700630 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 NULL
632};
633
Thomas Renninger8aa84ad2009-07-24 15:25:05 +0200634struct kobject *cpufreq_global_kobject;
635EXPORT_SYMBOL(cpufreq_global_kobject);
636
Dave Jones29464f22009-01-18 01:37:11 -0500637#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
638#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Dave Jones29464f22009-01-18 01:37:11 -0500640static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Dave Jones905d77c2008-03-05 14:28:32 -0500642 struct cpufreq_policy *policy = to_policy(kobj);
643 struct freq_attr *fattr = to_attr(attr);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500644 ssize_t ret = -EINVAL;
Stephen Boyda9144432012-07-20 18:14:38 +0000645 policy = cpufreq_cpu_get_sysfs(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (!policy)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500647 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800648
649 if (lock_policy_rwsem_read(policy->cpu) < 0)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500650 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800651
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530652 if (fattr->show)
653 ret = fattr->show(policy, buf);
654 else
655 ret = -EIO;
656
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800657 unlock_policy_rwsem_read(policy->cpu);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500658fail:
Stephen Boyda9144432012-07-20 18:14:38 +0000659 cpufreq_cpu_put_sysfs(policy);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500660no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return ret;
662}
663
Dave Jones905d77c2008-03-05 14:28:32 -0500664static ssize_t store(struct kobject *kobj, struct attribute *attr,
665 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Dave Jones905d77c2008-03-05 14:28:32 -0500667 struct cpufreq_policy *policy = to_policy(kobj);
668 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500669 ssize_t ret = -EINVAL;
Stephen Boyda9144432012-07-20 18:14:38 +0000670 policy = cpufreq_cpu_get_sysfs(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (!policy)
Dave Jonesa07530b2008-03-05 14:22:25 -0500672 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800673
674 if (lock_policy_rwsem_write(policy->cpu) < 0)
Dave Jonesa07530b2008-03-05 14:22:25 -0500675 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800676
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530677 if (fattr->store)
678 ret = fattr->store(policy, buf, count);
679 else
680 ret = -EIO;
681
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800682 unlock_policy_rwsem_write(policy->cpu);
Dave Jonesa07530b2008-03-05 14:22:25 -0500683fail:
Stephen Boyda9144432012-07-20 18:14:38 +0000684 cpufreq_cpu_put_sysfs(policy);
Dave Jonesa07530b2008-03-05 14:22:25 -0500685no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return ret;
687}
688
Dave Jones905d77c2008-03-05 14:28:32 -0500689static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Dave Jones905d77c2008-03-05 14:28:32 -0500691 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200692 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 complete(&policy->kobj_unregister);
694}
695
Emese Revfy52cf25d2010-01-19 02:58:23 +0100696static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 .show = show,
698 .store = store,
699};
700
701static struct kobj_type ktype_cpufreq = {
702 .sysfs_ops = &sysfs_ops,
703 .default_attrs = default_attrs,
704 .release = cpufreq_sysfs_release,
705};
706
Dave Jones19d6f7e2009-07-08 17:35:39 -0400707/* symlink affected CPUs */
Alex Chiangcf3289d02009-11-17 20:27:08 -0700708static int cpufreq_add_dev_symlink(unsigned int cpu,
709 struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400710{
711 unsigned int j;
712 int ret = 0;
713
714 for_each_cpu(j, policy->cpus) {
715 struct cpufreq_policy *managed_policy;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800716 struct device *cpu_dev;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400717
718 if (j == cpu)
719 continue;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400720
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200721 pr_debug("CPU %u already managed, adding link\n", j);
Dave Jones19d6f7e2009-07-08 17:35:39 -0400722 managed_policy = cpufreq_cpu_get(cpu);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800723 cpu_dev = get_cpu_device(j);
724 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Dave Jones19d6f7e2009-07-08 17:35:39 -0400725 "cpufreq");
726 if (ret) {
727 cpufreq_cpu_put(managed_policy);
728 return ret;
729 }
730 }
731 return ret;
732}
733
Alex Chiangcf3289d02009-11-17 20:27:08 -0700734static int cpufreq_add_dev_interface(unsigned int cpu,
735 struct cpufreq_policy *policy,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800736 struct device *dev)
Dave Jones909a6942009-07-08 18:05:42 -0400737{
Dave Jonesecf7e462009-07-08 18:48:47 -0400738 struct cpufreq_policy new_policy;
Dave Jones909a6942009-07-08 18:05:42 -0400739 struct freq_attr **drv_attr;
740 unsigned long flags;
741 int ret = 0;
742 unsigned int j;
743
744 /* prepare interface data */
745 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800746 &dev->kobj, "cpufreq");
Dave Jones909a6942009-07-08 18:05:42 -0400747 if (ret)
748 return ret;
749
750 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200751 drv_attr = cpufreq_driver->attr;
Dave Jones909a6942009-07-08 18:05:42 -0400752 while ((drv_attr) && (*drv_attr)) {
753 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
754 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200755 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400756 drv_attr++;
757 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200758 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -0400759 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
760 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200761 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400762 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200763 if (cpufreq_driver->target) {
Dave Jones909a6942009-07-08 18:05:42 -0400764 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
765 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200766 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400767 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200768 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +0100769 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
770 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200771 goto err_out_kobj_put;
Thomas Renningere2f74f32009-11-19 12:31:01 +0100772 }
Dave Jones909a6942009-07-08 18:05:42 -0400773
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000774 write_lock_irqsave(&cpufreq_driver_lock, flags);
Dave Jones909a6942009-07-08 18:05:42 -0400775 for_each_cpu(j, policy->cpus) {
Dave Jones909a6942009-07-08 18:05:42 -0400776 per_cpu(cpufreq_cpu_data, j) = policy;
Tejun Heof1625062009-10-29 22:34:13 +0900777 per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
Dave Jones909a6942009-07-08 18:05:42 -0400778 }
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000779 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones909a6942009-07-08 18:05:42 -0400780
781 ret = cpufreq_add_dev_symlink(cpu, policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400782 if (ret)
783 goto err_out_kobj_put;
784
785 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
786 /* assure that the starting sequence is run in __cpufreq_set_policy */
787 policy->governor = NULL;
788
789 /* set default policy */
790 ret = __cpufreq_set_policy(policy, &new_policy);
791 policy->user_policy.policy = policy->policy;
792 policy->user_policy.governor = policy->governor;
793
794 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200795 pr_debug("setting policy failed\n");
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200796 if (cpufreq_driver->exit)
797 cpufreq_driver->exit(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400798 }
Dave Jones909a6942009-07-08 18:05:42 -0400799 return ret;
800
801err_out_kobj_put:
802 kobject_put(&policy->kobj);
803 wait_for_completion(&policy->kobj_unregister);
804 return ret;
805}
806
Viresh Kumarfcf80582013-01-29 14:39:08 +0000807#ifdef CONFIG_HOTPLUG_CPU
808static int cpufreq_add_policy_cpu(unsigned int cpu, unsigned int sibling,
809 struct device *dev)
810{
811 struct cpufreq_policy *policy;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200812 int ret = 0, has_target = !!cpufreq_driver->target;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000813 unsigned long flags;
814
815 policy = cpufreq_cpu_get(sibling);
816 WARN_ON(!policy);
817
Viresh Kumar820c6ca2013-04-22 00:48:03 +0200818 if (has_target)
819 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000820
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530821 lock_policy_rwsem_write(sibling);
822
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000823 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530824
Viresh Kumarfcf80582013-01-29 14:39:08 +0000825 cpumask_set_cpu(cpu, policy->cpus);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530826 per_cpu(cpufreq_policy_cpu, cpu) = policy->cpu;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000827 per_cpu(cpufreq_cpu_data, cpu) = policy;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000828 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000829
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530830 unlock_policy_rwsem_write(sibling);
831
Viresh Kumar820c6ca2013-04-22 00:48:03 +0200832 if (has_target) {
833 __cpufreq_governor(policy, CPUFREQ_GOV_START);
834 __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
835 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000836
Viresh Kumarfcf80582013-01-29 14:39:08 +0000837 ret = sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
838 if (ret) {
839 cpufreq_cpu_put(policy);
840 return ret;
841 }
842
843 return 0;
844}
845#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847/**
848 * cpufreq_add_dev - add a CPU device
849 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500850 * Adds the cpufreq interface for a CPU device.
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400851 *
852 * The Oracle says: try running cpufreq registration/unregistration concurrently
853 * with with cpu hotplugging and all hell will break loose. Tried to clean this
854 * mess up, but more thorough testing is needed. - Mathieu
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800856static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
Viresh Kumarfcf80582013-01-29 14:39:08 +0000858 unsigned int j, cpu = dev->id;
Viresh Kumar65922462013-02-07 10:56:03 +0530859 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 unsigned long flags;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500862#ifdef CONFIG_HOTPLUG_CPU
Viresh Kumarfcf80582013-01-29 14:39:08 +0000863 struct cpufreq_governor *gov;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500864 int sibling;
865#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Ashok Rajc32b6b82005-10-30 14:59:54 -0800867 if (cpu_is_offline(cpu))
868 return 0;
869
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200870 pr_debug("adding CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872#ifdef CONFIG_SMP
873 /* check whether a different CPU already registered this
874 * CPU because it is in the same boat. */
875 policy = cpufreq_cpu_get(cpu);
876 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -0500877 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 return 0;
879 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000880
881#ifdef CONFIG_HOTPLUG_CPU
882 /* Check if this cpu was hot-unplugged earlier and has siblings */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000883 read_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000884 for_each_online_cpu(sibling) {
885 struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530886 if (cp && cpumask_test_cpu(cpu, cp->related_cpus)) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000887 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000888 return cpufreq_add_policy_cpu(cpu, sibling, dev);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530889 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000890 }
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000891 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000892#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893#endif
894
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200895 if (!try_module_get(cpufreq_driver->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 ret = -EINVAL;
897 goto module_out;
898 }
899
Dave Jonese98df502005-10-20 15:17:43 -0700900 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
Dave Jones059019a2009-07-08 16:30:03 -0400901 if (!policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 goto nomem_out;
Dave Jones059019a2009-07-08 16:30:03 -0400903
904 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400905 goto err_free_policy;
Dave Jones059019a2009-07-08 16:30:03 -0400906
907 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400908 goto err_free_cpumask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 policy->cpu = cpu;
Viresh Kumar65922462013-02-07 10:56:03 +0530911 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Rusty Russell835481d2009-01-04 05:18:06 -0800912 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800914 /* Initially set CPU itself as the policy_cpu */
Tejun Heof1625062009-10-29 22:34:13 +0900915 per_cpu(cpufreq_policy_cpu, cpu) = cpu;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 init_completion(&policy->kobj_unregister);
David Howells65f27f32006-11-22 14:55:48 +0000918 INIT_WORK(&policy->update, handle_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 /* call driver. From then on the cpufreq must be able
921 * to accept all calls to ->verify and ->setpolicy for this CPU
922 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200923 ret = cpufreq_driver->init(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200925 pr_debug("initialization failed\n");
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530926 goto err_set_policy_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
Viresh Kumar643ae6e2013-01-12 05:14:38 +0000928
Viresh Kumarfcf80582013-01-29 14:39:08 +0000929 /* related cpus should atleast have policy->cpus */
930 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
931
Viresh Kumar643ae6e2013-01-12 05:14:38 +0000932 /*
933 * affected cpus must always be the one, which are online. We aren't
934 * managing offline cpus here.
935 */
936 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
937
Mike Chan187d9f42008-12-04 12:19:17 -0800938 policy->user_policy.min = policy->min;
939 policy->user_policy.max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Thomas Renningera1531ac2008-07-29 22:32:58 -0700941 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
942 CPUFREQ_START, policy);
943
Viresh Kumarfcf80582013-01-29 14:39:08 +0000944#ifdef CONFIG_HOTPLUG_CPU
945 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
946 if (gov) {
947 policy->governor = gov;
948 pr_debug("Restoring governor %s for cpu %d\n",
949 policy->governor->name, cpu);
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200950 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000951#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800953 ret = cpufreq_add_dev_interface(cpu, policy, dev);
Dave Jones19d6f7e2009-07-08 17:35:39 -0400954 if (ret)
955 goto err_out_unregister;
Dave Jones8ff69732006-03-05 03:37:23 -0500956
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -0400957 kobject_uevent(&policy->kobj, KOBJ_ADD);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200958 module_put(cpufreq_driver->owner);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200959 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -0500960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return 0;
962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963err_out_unregister:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000964 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rusty Russell835481d2009-01-04 05:18:06 -0800965 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -0700966 per_cpu(cpufreq_cpu_data, j) = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000967 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800969 kobject_put(&policy->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 wait_for_completion(&policy->kobj_unregister);
971
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530972err_set_policy_cpu:
973 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Xiaotian Fengcad70a62010-07-20 20:11:02 +0800974 free_cpumask_var(policy->related_cpus);
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400975err_free_cpumask:
976 free_cpumask_var(policy->cpus);
977err_free_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 kfree(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979nomem_out:
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200980 module_put(cpufreq_driver->owner);
Ashok Rajc32b6b82005-10-30 14:59:54 -0800981module_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 return ret;
983}
984
Viresh Kumarb8eed8a2013-01-14 13:23:03 +0000985static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
986{
987 int j;
988
989 policy->last_cpu = policy->cpu;
990 policy->cpu = cpu;
991
Viresh Kumar3361b7b2013-02-04 11:38:51 +0000992 for_each_cpu(j, policy->cpus)
Viresh Kumarb8eed8a2013-01-14 13:23:03 +0000993 per_cpu(cpufreq_policy_cpu, j) = cpu;
Viresh Kumarb8eed8a2013-01-14 13:23:03 +0000994
995#ifdef CONFIG_CPU_FREQ_TABLE
996 cpufreq_frequency_table_update_policy_cpu(policy);
997#endif
998 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
999 CPUFREQ_UPDATE_POLICY_CPU, policy);
1000}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002/**
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001003 * __cpufreq_remove_dev - remove a CPU device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 *
1005 * Removes the cpufreq interface for a CPU device.
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001006 * Caller should already have policy_rwsem in write mode for this CPU.
1007 * This routine frees the rwsem before returning.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001009static int __cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001011 unsigned int cpu = dev->id, ret, cpus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 unsigned long flags;
1013 struct cpufreq_policy *data;
Amerigo Wang499bca92010-03-04 03:23:46 -05001014 struct kobject *kobj;
1015 struct completion *cmp;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001016 struct device *cpu_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001018 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001020 write_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 data = per_cpu(cpufreq_cpu_data, cpu);
Mike Travis7a6aedf2008-03-25 15:06:53 -07001023 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001025 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 if (!data) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001028 pr_debug("%s: No cpu_data found\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001032 if (cpufreq_driver->target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001034
Jacob Shin27ecddc2011-04-27 13:32:11 -05001035#ifdef CONFIG_HOTPLUG_CPU
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001036 if (!cpufreq_driver->setpolicy)
Dirk Brandewiefa69e332013-02-06 09:02:11 -08001037 strncpy(per_cpu(cpufreq_cpu_governor, cpu),
1038 data->governor->name, CPUFREQ_NAME_LEN);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001039#endif
1040
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301041 WARN_ON(lock_policy_rwsem_write(cpu));
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001042 cpus = cpumask_weight(data->cpus);
Viresh Kumare4969eb2013-04-11 08:04:53 +00001043
1044 if (cpus > 1)
1045 cpumask_clear_cpu(cpu, data->cpus);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301046 unlock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Viresh Kumar73bf0fc2013-02-05 22:21:14 +01001048 if (cpu != data->cpu) {
1049 sysfs_remove_link(&dev->kobj, "cpufreq");
1050 } else if (cpus > 1) {
Venki Pallipadiec282972007-03-26 12:03:19 -07001051 /* first sibling now owns the new sysfs dir */
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001052 cpu_dev = get_cpu_device(cpumask_first(data->cpus));
1053 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
1054 ret = kobject_move(&data->kobj, &cpu_dev->kobj);
1055 if (ret) {
1056 pr_err("%s: Failed to move kobj: %d", __func__, ret);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301057
1058 WARN_ON(lock_policy_rwsem_write(cpu));
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001059 cpumask_set_cpu(cpu, data->cpus);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301060
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001061 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301062 per_cpu(cpufreq_cpu_data, cpu) = data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001063 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301064
1065 unlock_policy_rwsem_write(cpu);
1066
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001067 ret = sysfs_create_link(&cpu_dev->kobj, &data->kobj,
1068 "cpufreq");
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001069 return -EINVAL;
1070 }
Venki Pallipadiec282972007-03-26 12:03:19 -07001071
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301072 WARN_ON(lock_policy_rwsem_write(cpu));
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001073 update_policy_cpu(data, cpu_dev->id);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301074 unlock_policy_rwsem_write(cpu);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001075 pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
1076 __func__, cpu_dev->id, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001077 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001078
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001079 /* If cpu is last user of policy, free policy */
1080 if (cpus == 1) {
Rafael J. Wysockie9ef4412013-07-30 00:32:00 +02001081 if (cpufreq_driver->target)
1082 __cpufreq_governor(data, CPUFREQ_GOV_POLICY_EXIT);
1083
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301084 lock_policy_rwsem_read(cpu);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001085 kobj = &data->kobj;
1086 cmp = &data->kobj_unregister;
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301087 unlock_policy_rwsem_read(cpu);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001088 kobject_put(kobj);
1089
1090 /* we need to make sure that the underlying kobj is actually
1091 * not referenced anymore by anybody before we proceed with
1092 * unloading.
1093 */
1094 pr_debug("waiting for dropping of refcount\n");
1095 wait_for_completion(cmp);
1096 pr_debug("wait complete\n");
1097
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001098 if (cpufreq_driver->exit)
1099 cpufreq_driver->exit(data);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001100
1101 free_cpumask_var(data->related_cpus);
1102 free_cpumask_var(data->cpus);
1103 kfree(data);
Rafael J. Wysockie9ef4412013-07-30 00:32:00 +02001104 } else {
1105 pr_debug("%s: removing link, cpu: %d\n", __func__, cpu);
1106 cpufreq_cpu_put(data);
1107 if (cpufreq_driver->target) {
1108 __cpufreq_governor(data, CPUFREQ_GOV_START);
1109 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1110 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301113 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 return 0;
1115}
1116
1117
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001118static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001119{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001120 unsigned int cpu = dev->id;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001121 int retval;
1122
1123 if (cpu_is_offline(cpu))
1124 return 0;
1125
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001126 retval = __cpufreq_remove_dev(dev, sif);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001127 return retval;
1128}
1129
1130
David Howells65f27f32006-11-22 14:55:48 +00001131static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
David Howells65f27f32006-11-22 14:55:48 +00001133 struct cpufreq_policy *policy =
1134 container_of(work, struct cpufreq_policy, update);
1135 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001136 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 cpufreq_update_policy(cpu);
1138}
1139
1140/**
1141 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
1142 * @cpu: cpu number
1143 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1144 * @new_freq: CPU frequency the CPU actually runs at
1145 *
Dave Jones29464f22009-01-18 01:37:11 -05001146 * We adjust to current frequency first, and need to clean up later.
1147 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301149static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1150 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301152 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301154 unsigned long flags;
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001157 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 freqs.old = old_freq;
1161 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301162
1163 read_lock_irqsave(&cpufreq_driver_lock, flags);
1164 policy = per_cpu(cpufreq_cpu_data, cpu);
1165 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1166
1167 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1168 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169}
1170
1171
Dave Jones32ee8c32006-02-28 00:43:23 -05001172/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301173 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001174 * @cpu: CPU number
1175 *
1176 * This is the last known freq, without actually getting it from the driver.
1177 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1178 */
1179unsigned int cpufreq_quick_get(unsigned int cpu)
1180{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001181 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301182 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001183
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001184 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1185 return cpufreq_driver->get(cpu);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001186
1187 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001188 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301189 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001190 cpufreq_cpu_put(policy);
1191 }
1192
Dave Jones4d34a672008-02-07 16:33:49 -05001193 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001194}
1195EXPORT_SYMBOL(cpufreq_quick_get);
1196
Jesse Barnes3d737102011-06-28 10:59:12 -07001197/**
1198 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1199 * @cpu: CPU number
1200 *
1201 * Just return the max possible frequency for a given CPU.
1202 */
1203unsigned int cpufreq_quick_get_max(unsigned int cpu)
1204{
1205 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1206 unsigned int ret_freq = 0;
1207
1208 if (policy) {
1209 ret_freq = policy->max;
1210 cpufreq_cpu_put(policy);
1211 }
1212
1213 return ret_freq;
1214}
1215EXPORT_SYMBOL(cpufreq_quick_get_max);
1216
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001217
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001218static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001220 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301221 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001223 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001224 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001226 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301228 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001229 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301230 /* verify no discrepancy between actual and
1231 saved value exists */
1232 if (unlikely(ret_freq != policy->cur)) {
1233 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 schedule_work(&policy->update);
1235 }
1236 }
1237
Dave Jones4d34a672008-02-07 16:33:49 -05001238 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001239}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001241/**
1242 * cpufreq_get - get the current CPU frequency (in kHz)
1243 * @cpu: CPU number
1244 *
1245 * Get the CPU current (static) CPU frequency
1246 */
1247unsigned int cpufreq_get(unsigned int cpu)
1248{
1249 unsigned int ret_freq = 0;
1250 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1251
1252 if (!policy)
1253 goto out;
1254
1255 if (unlikely(lock_policy_rwsem_read(cpu)))
1256 goto out_policy;
1257
1258 ret_freq = __cpufreq_get(cpu);
1259
1260 unlock_policy_rwsem_read(cpu);
1261
1262out_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 cpufreq_cpu_put(policy);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001264out:
Dave Jones4d34a672008-02-07 16:33:49 -05001265 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266}
1267EXPORT_SYMBOL(cpufreq_get);
1268
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001269static struct subsys_interface cpufreq_interface = {
1270 .name = "cpufreq",
1271 .subsys = &cpu_subsys,
1272 .add_dev = cpufreq_add_dev,
1273 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001274};
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277/**
Mark Browndb295c82015-03-09 12:26:45 +00001278 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001279 *
Mark Browndb295c82015-03-09 12:26:45 +00001280 * This function is only executed for the boot processor. The other CPUs
1281 * have been put offline by means of CPU hotplug.
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001282 */
Mark Browndb295c82015-03-09 12:26:45 +00001283static int cpufreq_bp_suspend(void)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001284{
Mark Browndb295c82015-03-09 12:26:45 +00001285 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001286
Mark Browndb295c82015-03-09 12:26:45 +00001287 int cpu = smp_processor_id();
1288 struct cpufreq_policy *cpu_policy;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001289
Mark Browndb295c82015-03-09 12:26:45 +00001290 pr_debug("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001291
Mark Browndb295c82015-03-09 12:26:45 +00001292 /* If there's no policy for the boot CPU, we have nothing to do. */
1293 cpu_policy = cpufreq_cpu_get(cpu);
1294 if (!cpu_policy)
1295 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001296
Mark Browndb295c82015-03-09 12:26:45 +00001297 if (cpufreq_driver->suspend) {
1298 ret = cpufreq_driver->suspend(cpu_policy);
1299 if (ret)
1300 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1301 "step on CPU %u\n", cpu_policy->cpu);
Mark Brownd255fca2015-03-09 12:25:38 +00001302 }
Mark Brownadb66502014-04-18 00:48:32 +01001303
Mark Browndb295c82015-03-09 12:26:45 +00001304 cpufreq_cpu_put(cpu_policy);
1305 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001306}
1307
1308/**
Mark Browndb295c82015-03-09 12:26:45 +00001309 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 *
Mark Browndb295c82015-03-09 12:26:45 +00001311 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1312 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1313 * restored. It will verify that the current freq is in sync with
1314 * what we believe it to be. This is a bit later than when it
1315 * should be, but nonethteless it's better than calling
1316 * cpufreq_driver->get() here which might re-enable interrupts...
1317 *
1318 * This function is only executed for the boot CPU. The other CPUs have not
1319 * been turned on yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 */
Mark Browndb295c82015-03-09 12:26:45 +00001321static void cpufreq_bp_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
Mark Browndb295c82015-03-09 12:26:45 +00001323 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001324
Mark Browndb295c82015-03-09 12:26:45 +00001325 int cpu = smp_processor_id();
1326 struct cpufreq_policy *cpu_policy;
1327
1328 pr_debug("resuming cpu %u\n", cpu);
1329
1330 /* If there's no policy for the boot CPU, we have nothing to do. */
1331 cpu_policy = cpufreq_cpu_get(cpu);
1332 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001333 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Mark Browndb295c82015-03-09 12:26:45 +00001335 if (cpufreq_driver->resume) {
1336 ret = cpufreq_driver->resume(cpu_policy);
1337 if (ret) {
1338 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1339 "step on CPU %u\n", cpu_policy->cpu);
1340 goto fail;
1341 }
Mark Brownd255fca2015-03-09 12:25:38 +00001342 }
Mark Brownadb66502014-04-18 00:48:32 +01001343
Mark Browndb295c82015-03-09 12:26:45 +00001344 schedule_work(&cpu_policy->update);
Mark Brownadb66502014-04-18 00:48:32 +01001345
Mark Browndb295c82015-03-09 12:26:45 +00001346fail:
1347 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348}
1349
Mark Browndb295c82015-03-09 12:26:45 +00001350static struct syscore_ops cpufreq_syscore_ops = {
1351 .suspend = cpufreq_bp_suspend,
1352 .resume = cpufreq_bp_resume,
1353};
1354
Borislav Petkov9d950462013-01-20 10:24:28 +00001355/**
1356 * cpufreq_get_current_driver - return current driver's name
1357 *
1358 * Return the name string of the currently loaded cpufreq driver
1359 * or NULL, if none.
1360 */
1361const char *cpufreq_get_current_driver(void)
1362{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001363 if (cpufreq_driver)
1364 return cpufreq_driver->name;
1365
1366 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001367}
1368EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370/*********************************************************************
1371 * NOTIFIER LISTS INTERFACE *
1372 *********************************************************************/
1373
1374/**
1375 * cpufreq_register_notifier - register a driver with cpufreq
1376 * @nb: notifier function to register
1377 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1378 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001379 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 * are notified about clock rate changes (once before and once after
1381 * the transition), or a list of drivers that are notified about
1382 * changes in cpufreq policy.
1383 *
1384 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001385 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 */
1387int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1388{
1389 int ret;
1390
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001391 if (cpufreq_disabled())
1392 return -EINVAL;
1393
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001394 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 switch (list) {
1397 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001398 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001399 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 break;
1401 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001402 ret = blocking_notifier_chain_register(
1403 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 break;
1405 default:
1406 ret = -EINVAL;
1407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 return ret;
1410}
1411EXPORT_SYMBOL(cpufreq_register_notifier);
1412
1413
1414/**
1415 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1416 * @nb: notifier block to be unregistered
1417 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1418 *
1419 * Remove a driver from the CPU frequency notifier list.
1420 *
1421 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001422 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 */
1424int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1425{
1426 int ret;
1427
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001428 if (cpufreq_disabled())
1429 return -EINVAL;
1430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 switch (list) {
1432 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001433 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001434 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 break;
1436 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001437 ret = blocking_notifier_chain_unregister(
1438 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 break;
1440 default:
1441 ret = -EINVAL;
1442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
1444 return ret;
1445}
1446EXPORT_SYMBOL(cpufreq_unregister_notifier);
1447
1448
1449/*********************************************************************
1450 * GOVERNORS *
1451 *********************************************************************/
1452
1453
1454int __cpufreq_driver_target(struct cpufreq_policy *policy,
1455 unsigned int target_freq,
1456 unsigned int relation)
1457{
1458 int retval = -EINVAL;
Viresh Kumar72499242012-10-31 01:28:21 +01001459 unsigned int old_target_freq = target_freq;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001460
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001461 if (cpufreq_disabled())
1462 return -ENODEV;
1463
Viresh Kumar72499242012-10-31 01:28:21 +01001464 /* Make sure that target_freq is within supported range */
1465 if (target_freq > policy->max)
1466 target_freq = policy->max;
1467 if (target_freq < policy->min)
1468 target_freq = policy->min;
1469
1470 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1471 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001472
1473 if (target_freq == policy->cur)
1474 return 0;
1475
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001476 if (cpufreq_driver->target)
1477 retval = cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 return retval;
1480}
1481EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483int cpufreq_driver_target(struct cpufreq_policy *policy,
1484 unsigned int target_freq,
1485 unsigned int relation)
1486{
Julia Lawallf1829e42008-07-25 22:44:53 +02001487 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 policy = cpufreq_cpu_get(policy->cpu);
1490 if (!policy)
Julia Lawallf1829e42008-07-25 22:44:53 +02001491 goto no_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001493 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
Julia Lawallf1829e42008-07-25 22:44:53 +02001494 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
1496 ret = __cpufreq_driver_target(policy, target_freq, relation);
1497
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001498 unlock_policy_rwsem_write(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Julia Lawallf1829e42008-07-25 22:44:53 +02001500fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 cpufreq_cpu_put(policy);
Julia Lawallf1829e42008-07-25 22:44:53 +02001502no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 return ret;
1504}
1505EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1506
venkatesh.pallipadi@intel.combf0b90e2008-08-04 11:59:07 -07001507int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu)
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001508{
1509 int ret = 0;
1510
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001511 if (cpufreq_disabled())
1512 return ret;
1513
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001514 if (!cpufreq_driver->getavg)
Viresh Kumar0676f7f2012-10-24 23:39:48 +02001515 return 0;
1516
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001517 policy = cpufreq_cpu_get(policy->cpu);
1518 if (!policy)
1519 return -EINVAL;
1520
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001521 ret = cpufreq_driver->getavg(policy, cpu);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001522
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001523 cpufreq_cpu_put(policy);
1524 return ret;
1525}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001526EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001527
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001528/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001529 * when "event" is CPUFREQ_GOV_LIMITS
1530 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301532static int __cpufreq_governor(struct cpufreq_policy *policy,
1533 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
Dave Jonescc993ca2005-07-28 09:43:56 -07001535 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001536
1537 /* Only must be defined when default governor is known to have latency
1538 restrictions, like e.g. conservative or ondemand.
1539 That this is the case is already ensured in Kconfig
1540 */
1541#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1542 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1543#else
1544 struct cpufreq_governor *gov = NULL;
1545#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001546
1547 if (policy->governor->max_transition_latency &&
1548 policy->cpuinfo.transition_latency >
1549 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07001550 if (!gov)
1551 return -EINVAL;
1552 else {
1553 printk(KERN_WARNING "%s governor failed, too long"
1554 " transition latency of HW, fallback"
1555 " to %s governor\n",
1556 policy->governor->name,
1557 gov->name);
1558 policy->governor = gov;
1559 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 if (!try_module_get(policy->governor->owner))
1563 return -EINVAL;
1564
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001565 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301566 policy->cpu, event);
Xiaoguang Chenba17ca42013-06-19 15:00:07 +08001567
1568 mutex_lock(&cpufreq_governor_lock);
1569 if ((!policy->governor_enabled && (event == CPUFREQ_GOV_STOP)) ||
1570 (policy->governor_enabled && (event == CPUFREQ_GOV_START))) {
1571 mutex_unlock(&cpufreq_governor_lock);
1572 return -EBUSY;
1573 }
1574
1575 if (event == CPUFREQ_GOV_STOP)
1576 policy->governor_enabled = false;
1577 else if (event == CPUFREQ_GOV_START)
1578 policy->governor_enabled = true;
1579
1580 mutex_unlock(&cpufreq_governor_lock);
1581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 ret = policy->governor->governor(policy, event);
1583
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001584 if (!ret) {
1585 if (event == CPUFREQ_GOV_POLICY_INIT)
1586 policy->governor->initialized++;
1587 else if (event == CPUFREQ_GOV_POLICY_EXIT)
1588 policy->governor->initialized--;
Xiaoguang Chenba17ca42013-06-19 15:00:07 +08001589 } else {
1590 /* Restore original values */
1591 mutex_lock(&cpufreq_governor_lock);
1592 if (event == CPUFREQ_GOV_STOP)
1593 policy->governor_enabled = true;
1594 else if (event == CPUFREQ_GOV_START)
1595 policy->governor_enabled = false;
1596 mutex_unlock(&cpufreq_governor_lock);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001597 }
Viresh Kumarb3940582013-02-01 05:42:58 +00001598
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301599 /* we keep one module reference alive for
1600 each CPU governed by this CPU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 if ((event != CPUFREQ_GOV_START) || ret)
1602 module_put(policy->governor->owner);
1603 if ((event == CPUFREQ_GOV_STOP) && !ret)
1604 module_put(policy->governor->owner);
1605
1606 return ret;
1607}
1608
1609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610int cpufreq_register_governor(struct cpufreq_governor *governor)
1611{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001612 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
1614 if (!governor)
1615 return -EINVAL;
1616
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001617 if (cpufreq_disabled())
1618 return -ENODEV;
1619
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001620 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001621
Viresh Kumarb3940582013-02-01 05:42:58 +00001622 governor->initialized = 0;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001623 err = -EBUSY;
1624 if (__find_governor(governor->name) == NULL) {
1625 err = 0;
1626 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Dave Jones32ee8c32006-02-28 00:43:23 -05001629 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001630 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631}
1632EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1633
1634
1635void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1636{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001637#ifdef CONFIG_HOTPLUG_CPU
1638 int cpu;
1639#endif
1640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 if (!governor)
1642 return;
1643
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001644 if (cpufreq_disabled())
1645 return;
1646
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001647#ifdef CONFIG_HOTPLUG_CPU
1648 for_each_present_cpu(cpu) {
1649 if (cpu_online(cpu))
1650 continue;
1651 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1652 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1653 }
1654#endif
1655
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001656 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001658 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 return;
1660}
1661EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1662
1663
1664
1665/*********************************************************************
1666 * POLICY INTERFACE *
1667 *********************************************************************/
1668
1669/**
1670 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05001671 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1672 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 *
1674 * Reads the current cpufreq policy.
1675 */
1676int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1677{
1678 struct cpufreq_policy *cpu_policy;
1679 if (!policy)
1680 return -EINVAL;
1681
1682 cpu_policy = cpufreq_cpu_get(cpu);
1683 if (!cpu_policy)
1684 return -EINVAL;
1685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
1688 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 return 0;
1690}
1691EXPORT_SYMBOL(cpufreq_get_policy);
1692
1693
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001694/*
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301695 * data : current policy.
1696 * policy : policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001697 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301698static int __cpufreq_set_policy(struct cpufreq_policy *data,
1699 struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001701 int ret = 0, failed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001703 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 policy->min, policy->max);
1705
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301706 memcpy(&policy->cpuinfo, &data->cpuinfo,
1707 sizeof(struct cpufreq_cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Yi Yang53391fa2008-01-30 13:33:34 +01001709 if (policy->min > data->max || policy->max < data->min) {
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02001710 ret = -EINVAL;
1711 goto error_out;
1712 }
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 /* verify the cpu speed can be set within this limit */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001715 ret = cpufreq_driver->verify(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 if (ret)
1717 goto error_out;
1718
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001720 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1721 CPUFREQ_ADJUST, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
1723 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001724 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1725 CPUFREQ_INCOMPATIBLE, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
1727 /* verify the cpu speed can be set within this limit,
1728 which might be different to the first one */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001729 ret = cpufreq_driver->verify(policy);
Alan Sterne041c682006-03-27 01:16:30 -08001730 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001734 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1735 CPUFREQ_NOTIFY, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Dave Jones7d5e3502006-02-02 17:03:42 -05001737 data->min = policy->min;
1738 data->max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001740 pr_debug("new min and max freqs are %u - %u kHz\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301741 data->min, data->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001743 if (cpufreq_driver->setpolicy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 data->policy = policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001745 pr_debug("setting range\n");
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001746 ret = cpufreq_driver->setpolicy(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 } else {
1748 if (policy->governor != data->governor) {
1749 /* save old, working values */
1750 struct cpufreq_governor *old_gov = data->governor;
1751
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001752 pr_debug("governor switch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
1754 /* end old governor */
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001755 if (data->governor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Viresh Kumar955ef482013-05-16 05:09:58 +00001757 unlock_policy_rwsem_write(policy->cpu);
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001758 __cpufreq_governor(data,
1759 CPUFREQ_GOV_POLICY_EXIT);
Viresh Kumar955ef482013-05-16 05:09:58 +00001760 lock_policy_rwsem_write(policy->cpu);
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
1763 /* start new governor */
1764 data->governor = policy->governor;
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001765 if (!__cpufreq_governor(data, CPUFREQ_GOV_POLICY_INIT)) {
Viresh Kumar955ef482013-05-16 05:09:58 +00001766 if (!__cpufreq_governor(data, CPUFREQ_GOV_START)) {
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001767 failed = 0;
Viresh Kumar955ef482013-05-16 05:09:58 +00001768 } else {
1769 unlock_policy_rwsem_write(policy->cpu);
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001770 __cpufreq_governor(data,
1771 CPUFREQ_GOV_POLICY_EXIT);
Viresh Kumar955ef482013-05-16 05:09:58 +00001772 lock_policy_rwsem_write(policy->cpu);
1773 }
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001774 }
1775
1776 if (failed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 /* new governor failed, so re-start old one */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001778 pr_debug("starting governor %s failed\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301779 data->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 if (old_gov) {
1781 data->governor = old_gov;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301782 __cpufreq_governor(data,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001783 CPUFREQ_GOV_POLICY_INIT);
1784 __cpufreq_governor(data,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301785 CPUFREQ_GOV_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 }
1787 ret = -EINVAL;
1788 goto error_out;
1789 }
1790 /* might be a policy change, too, so fall through */
1791 }
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001792 pr_debug("governor: change or update limits\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1794 }
1795
Dave Jones7d5e3502006-02-02 17:03:42 -05001796error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 return ret;
1798}
1799
1800/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1802 * @cpu: CPU which shall be re-evaluated
1803 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001804 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 * at different times.
1806 */
1807int cpufreq_update_policy(unsigned int cpu)
1808{
1809 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1810 struct cpufreq_policy policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02001811 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Julia Lawallf1829e42008-07-25 22:44:53 +02001813 if (!data) {
1814 ret = -ENODEV;
1815 goto no_policy;
1816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Julia Lawallf1829e42008-07-25 22:44:53 +02001818 if (unlikely(lock_policy_rwsem_write(cpu))) {
1819 ret = -EINVAL;
1820 goto fail;
1821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001823 pr_debug("updating policy for CPU %u\n", cpu);
Dave Jones7d5e3502006-02-02 17:03:42 -05001824 memcpy(&policy, data, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 policy.min = data->user_policy.min;
1826 policy.max = data->user_policy.max;
1827 policy.policy = data->user_policy.policy;
1828 policy.governor = data->user_policy.governor;
1829
Thomas Renninger0961dd02006-01-26 18:46:33 +01001830 /* BIOS might change freq behind our back
1831 -> ask driver for current freq and notify governors about a change */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001832 if (cpufreq_driver->get) {
1833 policy.cur = cpufreq_driver->get(cpu);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001834 if (!data->cur) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001835 pr_debug("Driver did not initialize current freq");
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001836 data->cur = policy.cur;
1837 } else {
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001838 if (data->cur != policy.cur && cpufreq_driver->target)
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301839 cpufreq_out_of_sync(cpu, data->cur,
1840 policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001841 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01001842 }
1843
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 ret = __cpufreq_set_policy(data, &policy);
1845
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001846 unlock_policy_rwsem_write(cpu);
1847
Julia Lawallf1829e42008-07-25 22:44:53 +02001848fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 cpufreq_cpu_put(data);
Julia Lawallf1829e42008-07-25 22:44:53 +02001850no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 return ret;
1852}
1853EXPORT_SYMBOL(cpufreq_update_policy);
1854
Satyam Sharmadd184a02007-10-02 13:28:14 -07001855static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08001856 unsigned long action, void *hcpu)
1857{
1858 unsigned int cpu = (unsigned long)hcpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001859 struct device *dev;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001860
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001861 dev = get_cpu_device(cpu);
1862 if (dev) {
Ashok Rajc32b6b82005-10-30 14:59:54 -08001863 switch (action) {
1864 case CPU_ONLINE:
Srivatsa S. Bhat9d3ce4a2013-07-12 03:45:37 +05301865 case CPU_ONLINE_FROZEN:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001866 cpufreq_add_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001867 break;
1868 case CPU_DOWN_PREPARE:
Srivatsa S. Bhat9d3ce4a2013-07-12 03:45:37 +05301869 case CPU_DOWN_PREPARE_FROZEN:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001870 __cpufreq_remove_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001871 break;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001872 case CPU_DOWN_FAILED:
Srivatsa S. Bhat9d3ce4a2013-07-12 03:45:37 +05301873 case CPU_DOWN_FAILED_FROZEN:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001874 cpufreq_add_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001875 break;
1876 }
1877 }
1878 return NOTIFY_OK;
1879}
1880
Neal Buckendahl9c36f742010-06-22 22:02:44 -05001881static struct notifier_block __refdata cpufreq_cpu_notifier = {
Ashok Rajc32b6b82005-10-30 14:59:54 -08001882 .notifier_call = cpufreq_cpu_callback,
1883};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
1885/*********************************************************************
1886 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1887 *********************************************************************/
1888
1889/**
1890 * cpufreq_register_driver - register a CPU Frequency driver
1891 * @driver_data: A struct cpufreq_driver containing the values#
1892 * submitted by the CPU Frequency driver.
1893 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001894 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05001896 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 *
1898 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001899int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900{
1901 unsigned long flags;
1902 int ret;
1903
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001904 if (cpufreq_disabled())
1905 return -ENODEV;
1906
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 if (!driver_data || !driver_data->verify || !driver_data->init ||
1908 ((!driver_data->setpolicy) && (!driver_data->target)))
1909 return -EINVAL;
1910
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001911 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
1913 if (driver_data->setpolicy)
1914 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1915
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001916 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001917 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001918 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 return -EBUSY;
1920 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001921 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001922 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001924 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001925 if (ret)
1926 goto err_null_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001928 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 int i;
1930 ret = -ENODEV;
1931
1932 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07001933 for (i = 0; i < nr_cpu_ids; i++)
1934 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07001936 break;
1937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
1939 /* if all ->init() calls failed, unregister */
1940 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001941 pr_debug("no CPU initialized for driver %s\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301942 driver_data->name);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001943 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 }
1945 }
1946
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001947 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001948 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001950 return 0;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001951err_if_unreg:
1952 subsys_interface_unregister(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001953err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001954 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001955 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001956 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05001957 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958}
1959EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1960
1961
1962/**
1963 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1964 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001965 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 * the right to do so, i.e. if you have succeeded in initialising before!
1967 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1968 * currently not initialised.
1969 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001970int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
1972 unsigned long flags;
1973
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001974 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001977 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001979 subsys_interface_unregister(&cpufreq_interface);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001980 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001982 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001983 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001984 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
1986 return 0;
1987}
1988EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001989
1990static int __init cpufreq_core_init(void)
1991{
1992 int cpu;
1993
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001994 if (cpufreq_disabled())
1995 return -ENODEV;
1996
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001997 for_each_possible_cpu(cpu) {
Tejun Heof1625062009-10-29 22:34:13 +09001998 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001999 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
2000 }
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002001
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002002 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002003 BUG_ON(!cpufreq_global_kobject);
Mark Browndb295c82015-03-09 12:26:45 +00002004 register_syscore_ops(&cpufreq_syscore_ops);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002005
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002006 return 0;
2007}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002008core_initcall(cpufreq_core_init);