blob: 016294db2a795079ac1b7a819d4d37a311f140fe [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
Viresh Kumara5d035d2013-05-16 14:17:11 +053020#include <asm/cputime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/kernel.h>
Viresh Kumara5d035d2013-05-16 14:17:11 +053022#include <linux/kernel_stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/module.h>
24#include <linux/init.h>
25#include <linux/notifier.h>
26#include <linux/cpufreq.h>
27#include <linux/delay.h>
28#include <linux/interrupt.h>
29#include <linux/spinlock.h>
Viresh Kumara5d035d2013-05-16 14:17:11 +053030#include <linux/tick.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/device.h>
32#include <linux/slab.h>
33#include <linux/cpu.h>
34#include <linux/completion.h>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080035#include <linux/mutex.h>
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +010036#include <linux/syscore_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Thomas Renninger6f4f2722010-04-20 13:17:36 +020038#include <trace/events/power.h>
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/**
Dave Jonescd878472006-08-11 17:59:28 -040041 * The "cpufreq driver" - the arch- or hardware-dependent low
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 * level driver of CPUFreq support, and its spinlock. This lock
43 * also protects the cpufreq_cpu_data array.
44 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +020045static struct cpufreq_driver *cpufreq_driver;
Mike Travis7a6aedf2008-03-25 15:06:53 -070046static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
Thomas Renninger084f3492007-07-09 11:35:28 -070047#ifdef CONFIG_HOTPLUG_CPU
48/* This one keeps track of the previously set governor of a removed CPU */
Dmitry Monakhove77b89f2009-10-05 00:38:55 +040049static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
Thomas Renninger084f3492007-07-09 11:35:28 -070050#endif
Nathan Zimmer0d1857a2013-02-22 16:24:34 +000051static DEFINE_RWLOCK(cpufreq_driver_lock);
Xiaoguang Chenba17ca42013-06-19 15:00:07 +080052static DEFINE_MUTEX(cpufreq_governor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080054/*
55 * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
56 * all cpufreq/hotplug/workqueue/etc related lock issues.
57 *
58 * The rules for this semaphore:
59 * - Any routine that wants to read from the policy structure will
60 * do a down_read on this semaphore.
61 * - Any routine that will write to the policy structure and/or may take away
62 * the policy altogether (eg. CPU hotplug), will hold this lock in write
63 * mode before doing so.
64 *
65 * Additional rules:
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080066 * - Governor routines that can be called in cpufreq hotplug path should not
67 * take this sem as top level hotplug notifier handler takes this.
Mathieu Desnoyers395913d2009-06-08 13:17:31 -040068 * - Lock should not be held across
69 * __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080070 */
Tejun Heof1625062009-10-29 22:34:13 +090071static DEFINE_PER_CPU(int, cpufreq_policy_cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080072static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
73
74#define lock_policy_rwsem(mode, cpu) \
Viresh Kumarfa1d8af2013-02-07 15:38:42 +053075static int lock_policy_rwsem_##mode(int cpu) \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080076{ \
Tejun Heof1625062009-10-29 22:34:13 +090077 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080078 BUG_ON(policy_cpu == -1); \
79 down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080080 \
81 return 0; \
82}
83
84lock_policy_rwsem(read, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080085lock_policy_rwsem(write, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080086
Viresh Kumarfa1d8af2013-02-07 15:38:42 +053087#define unlock_policy_rwsem(mode, cpu) \
88static void unlock_policy_rwsem_##mode(int cpu) \
89{ \
90 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
91 BUG_ON(policy_cpu == -1); \
92 up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080093}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080094
Viresh Kumarfa1d8af2013-02-07 15:38:42 +053095unlock_policy_rwsem(read, cpu);
96unlock_policy_rwsem(write, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/* internal prototypes */
Dave Jones29464f22009-01-18 01:37:11 -050099static int __cpufreq_governor(struct cpufreq_policy *policy,
100 unsigned int event);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800101static unsigned int __cpufreq_get(unsigned int cpu);
David Howells65f27f32006-11-22 14:55:48 +0000102static void handle_update(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104/**
Dave Jones32ee8c32006-02-28 00:43:23 -0500105 * Two notifier lists: the "policy" list is involved in the
106 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * "transition" list for kernel code that needs to handle
108 * changes to devices when the CPU clock speed changes.
109 * The mutex locks both lists.
110 */
Alan Sterne041c682006-03-27 01:16:30 -0800111static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700112static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200114static bool init_cpufreq_transition_notifier_list_called;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700115static int __init init_cpufreq_transition_notifier_list(void)
116{
117 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200118 init_cpufreq_transition_notifier_list_called = true;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700119 return 0;
120}
Linus Torvaldsb3438f82006-11-20 11:47:18 -0800121pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400123static int off __read_mostly;
Viresh Kumarda584452012-10-26 00:51:32 +0200124static int cpufreq_disabled(void)
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400125{
126 return off;
127}
128void disable_cpufreq(void)
129{
130 off = 1;
131}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132static LIST_HEAD(cpufreq_governor_list);
Dave Jones29464f22009-01-18 01:37:11 -0500133static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000135bool have_governor_per_policy(void)
136{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200137 return cpufreq_driver->have_governor_per_policy;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000138}
Viresh Kumar79973322013-05-13 16:31:04 +0530139EXPORT_SYMBOL_GPL(have_governor_per_policy);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000140
Viresh Kumar69bbfa92013-05-13 16:26:48 +0530141struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
142{
143 if (have_governor_per_policy())
144 return &policy->kobj;
145 else
146 return cpufreq_global_kobject;
147}
148EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
149
Viresh Kumara5d035d2013-05-16 14:17:11 +0530150static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
151{
152 u64 idle_time;
153 u64 cur_wall_time;
154 u64 busy_time;
155
156 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
157
158 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
159 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
160 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
161 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
162 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
163 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
164
165 idle_time = cur_wall_time - busy_time;
166 if (wall)
167 *wall = cputime_to_usecs(cur_wall_time);
168
169 return cputime_to_usecs(idle_time);
170}
171
172u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
173{
174 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
175
176 if (idle_time == -1ULL)
177 return get_cpu_idle_time_jiffy(cpu, wall);
178 else if (!io_busy)
179 idle_time += get_cpu_iowait_time_us(cpu, wall);
180
181 return idle_time;
182}
183EXPORT_SYMBOL_GPL(get_cpu_idle_time);
184
Stephen Boyda9144432012-07-20 18:14:38 +0000185static struct cpufreq_policy *__cpufreq_cpu_get(unsigned int cpu, bool sysfs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 struct cpufreq_policy *data;
188 unsigned long flags;
189
Mike Travis7a6aedf2008-03-25 15:06:53 -0700190 if (cpu >= nr_cpu_ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 goto err_out;
192
193 /* get the cpufreq driver */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000194 read_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200196 if (!cpufreq_driver)
197 goto err_out_unlock;
198
199 if (!try_module_get(cpufreq_driver->owner))
200 goto err_out_unlock;
201
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 /* get the CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -0700204 data = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 if (!data)
207 goto err_out_put_module;
208
Stephen Boyda9144432012-07-20 18:14:38 +0000209 if (!sysfs && !kobject_get(&data->kobj))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 goto err_out_put_module;
211
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000212 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return data;
214
Dave Jones7d5e3502006-02-02 17:03:42 -0500215err_out_put_module:
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200216 module_put(cpufreq_driver->owner);
Nathan Zimmer58000432013-04-04 14:53:25 +0000217err_out_unlock:
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200218 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones7d5e3502006-02-02 17:03:42 -0500219err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return NULL;
221}
Stephen Boyda9144432012-07-20 18:14:38 +0000222
223struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
224{
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000225 if (cpufreq_disabled())
226 return NULL;
227
Stephen Boyda9144432012-07-20 18:14:38 +0000228 return __cpufreq_cpu_get(cpu, false);
229}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
231
Stephen Boyda9144432012-07-20 18:14:38 +0000232static struct cpufreq_policy *cpufreq_cpu_get_sysfs(unsigned int cpu)
233{
234 return __cpufreq_cpu_get(cpu, true);
235}
236
237static void __cpufreq_cpu_put(struct cpufreq_policy *data, bool sysfs)
238{
239 if (!sysfs)
240 kobject_put(&data->kobj);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200241 module_put(cpufreq_driver->owner);
Stephen Boyda9144432012-07-20 18:14:38 +0000242}
Dave Jones7d5e3502006-02-02 17:03:42 -0500243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244void cpufreq_cpu_put(struct cpufreq_policy *data)
245{
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000246 if (cpufreq_disabled())
247 return;
248
Stephen Boyda9144432012-07-20 18:14:38 +0000249 __cpufreq_cpu_put(data, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
252
Stephen Boyda9144432012-07-20 18:14:38 +0000253static void cpufreq_cpu_put_sysfs(struct cpufreq_policy *data)
254{
255 __cpufreq_cpu_put(data, true);
256}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
260 *********************************************************************/
261
262/**
263 * adjust_jiffies - adjust the system "loops_per_jiffy"
264 *
265 * This function alters the system "loops_per_jiffy" for the clock
266 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500267 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 * per-CPU loops_per_jiffy value wherever possible.
269 */
270#ifndef CONFIG_SMP
271static unsigned long l_p_j_ref;
272static unsigned int l_p_j_ref_freq;
273
Arjan van de Ven858119e2006-01-14 13:20:43 -0800274static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 if (ci->flags & CPUFREQ_CONST_LOOPS)
277 return;
278
279 if (!l_p_j_ref_freq) {
280 l_p_j_ref = loops_per_jiffy;
281 l_p_j_ref_freq = ci->old;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200282 pr_debug("saving %lu as reference value for loops_per_jiffy; "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530283 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
Afzal Mohammedd08de0c12012-01-04 10:52:46 +0530285 if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700286 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530287 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
288 ci->new);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200289 pr_debug("scaling loops_per_jiffy to %lu "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530290 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292}
293#else
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530294static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
295{
296 return;
297}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298#endif
299
300
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530301void __cpufreq_notify_transition(struct cpufreq_policy *policy,
302 struct cpufreq_freqs *freqs, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 BUG_ON(irqs_disabled());
305
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000306 if (cpufreq_disabled())
307 return;
308
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200309 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200310 pr_debug("notification %u of frequency transition to %u kHz\n",
Dave Jonese4472cb2006-01-31 15:53:55 -0800311 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500316 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800317 * which is not equal to what the cpufreq core thinks is
318 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200320 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800321 if ((policy) && (policy->cpu == freqs->cpu) &&
322 (policy->cur) && (policy->cur != freqs->old)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200323 pr_debug("Warning: CPU frequency is"
Dave Jonese4472cb2006-01-31 15:53:55 -0800324 " %u, cpufreq assumed %u kHz.\n",
325 freqs->old, policy->cur);
326 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
328 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700329 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800330 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
332 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 case CPUFREQ_POSTCHANGE:
335 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200336 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
Thomas Renninger6f4f2722010-04-20 13:17:36 +0200337 (unsigned long)freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100338 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700339 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800340 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800341 if (likely(policy) && likely(policy->cpu == freqs->cpu))
342 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 break;
344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530346/**
347 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
348 * on frequency transition.
349 *
350 * This function calls the transition notifiers and the "adjust_jiffies"
351 * function. It is called twice on all CPU frequency changes that have
352 * external effects.
353 */
354void cpufreq_notify_transition(struct cpufreq_policy *policy,
355 struct cpufreq_freqs *freqs, unsigned int state)
356{
357 for_each_cpu(freqs->cpu, policy->cpus)
358 __cpufreq_notify_transition(policy, freqs, state);
359}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
361
362
363
364/*********************************************************************
365 * SYSFS INTERFACE *
366 *********************************************************************/
367
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700368static struct cpufreq_governor *__find_governor(const char *str_governor)
369{
370 struct cpufreq_governor *t;
371
372 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
Dave Jones29464f22009-01-18 01:37:11 -0500373 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700374 return t;
375
376 return NULL;
377}
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379/**
380 * cpufreq_parse_governor - parse a governor string
381 */
Dave Jones905d77c2008-03-05 14:28:32 -0500382static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 struct cpufreq_governor **governor)
384{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700385 int err = -EINVAL;
386
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200387 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700388 goto out;
389
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200390 if (cpufreq_driver->setpolicy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
392 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700393 err = 0;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530394 } else if (!strnicmp(str_governor, "powersave",
395 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700397 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200399 } else if (cpufreq_driver->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700401
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800402 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700403
404 t = __find_governor(str_governor);
405
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700406 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700407 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700408
Kees Cook1a8e1462011-05-04 08:38:56 -0700409 mutex_unlock(&cpufreq_governor_mutex);
410 ret = request_module("cpufreq_%s", str_governor);
411 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700412
Kees Cook1a8e1462011-05-04 08:38:56 -0700413 if (ret == 0)
414 t = __find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700415 }
416
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700417 if (t != NULL) {
418 *governor = t;
419 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700421
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800422 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
Dave Jones29464f22009-01-18 01:37:11 -0500424out:
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700425 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530430 * cpufreq_per_cpu_attr_read() / show_##file_name() -
431 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 *
433 * Write out information from cpufreq_driver->policy[cpu]; object must be
434 * "unsigned int".
435 */
436
Dave Jones32ee8c32006-02-28 00:43:23 -0500437#define show_one(file_name, object) \
438static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500439(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500440{ \
Dave Jones29464f22009-01-18 01:37:11 -0500441 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
444show_one(cpuinfo_min_freq, cpuinfo.min_freq);
445show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100446show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447show_one(scaling_min_freq, min);
448show_one(scaling_max_freq, max);
449show_one(scaling_cur_freq, cur);
450
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530451static int __cpufreq_set_policy(struct cpufreq_policy *data,
452 struct cpufreq_policy *policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454/**
455 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
456 */
457#define store_one(file_name, object) \
458static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500459(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{ \
Jingoo Hanf55c9c22012-10-31 05:49:13 +0000461 unsigned int ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 struct cpufreq_policy new_policy; \
463 \
464 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
465 if (ret) \
466 return -EINVAL; \
467 \
Dave Jones29464f22009-01-18 01:37:11 -0500468 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (ret != 1) \
470 return -EINVAL; \
471 \
Thomas Renninger7970e082006-04-13 15:14:04 +0200472 ret = __cpufreq_set_policy(policy, &new_policy); \
473 policy->user_policy.object = policy->object; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 \
475 return ret ? ret : count; \
476}
477
Dave Jones29464f22009-01-18 01:37:11 -0500478store_one(scaling_min_freq, min);
479store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481/**
482 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
483 */
Dave Jones905d77c2008-03-05 14:28:32 -0500484static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
485 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800487 unsigned int cur_freq = __cpufreq_get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (!cur_freq)
489 return sprintf(buf, "<unknown>");
490 return sprintf(buf, "%u\n", cur_freq);
491}
492
493
494/**
495 * show_scaling_governor - show the current policy for the specified CPU
496 */
Dave Jones905d77c2008-03-05 14:28:32 -0500497static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Dave Jones29464f22009-01-18 01:37:11 -0500499 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return sprintf(buf, "powersave\n");
501 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
502 return sprintf(buf, "performance\n");
503 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200504 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500505 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return -EINVAL;
507}
508
509
510/**
511 * store_scaling_governor - store policy for the specified CPU
512 */
Dave Jones905d77c2008-03-05 14:28:32 -0500513static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
514 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Jingoo Hanf55c9c22012-10-31 05:49:13 +0000516 unsigned int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 char str_governor[16];
518 struct cpufreq_policy new_policy;
519
520 ret = cpufreq_get_policy(&new_policy, policy->cpu);
521 if (ret)
522 return ret;
523
Dave Jones29464f22009-01-18 01:37:11 -0500524 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if (ret != 1)
526 return -EINVAL;
527
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530528 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
529 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return -EINVAL;
531
Thomas Renninger7970e082006-04-13 15:14:04 +0200532 /* Do not use cpufreq_set_policy here or the user_policy.max
533 will be wrongly overridden */
Thomas Renninger7970e082006-04-13 15:14:04 +0200534 ret = __cpufreq_set_policy(policy, &new_policy);
535
536 policy->user_policy.policy = policy->policy;
537 policy->user_policy.governor = policy->governor;
Thomas Renninger7970e082006-04-13 15:14:04 +0200538
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530539 if (ret)
540 return ret;
541 else
542 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
545/**
546 * show_scaling_driver - show the cpufreq driver currently loaded
547 */
Dave Jones905d77c2008-03-05 14:28:32 -0500548static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200550 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
553/**
554 * show_scaling_available_governors - show the available CPUfreq governors
555 */
Dave Jones905d77c2008-03-05 14:28:32 -0500556static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
557 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
559 ssize_t i = 0;
560 struct cpufreq_governor *t;
561
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200562 if (!cpufreq_driver->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 i += sprintf(buf, "performance powersave");
564 goto out;
565 }
566
567 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
Dave Jones29464f22009-01-18 01:37:11 -0500568 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
569 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200571 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500573out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 i += sprintf(&buf[i], "\n");
575 return i;
576}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700577
Rusty Russell835481d2009-01-04 05:18:06 -0800578static ssize_t show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 ssize_t i = 0;
581 unsigned int cpu;
582
Rusty Russell835481d2009-01-04 05:18:06 -0800583 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (i)
585 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
586 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
587 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500588 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
590 i += sprintf(&buf[i], "\n");
591 return i;
592}
593
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700594/**
595 * show_related_cpus - show the CPUs affected by each transition even if
596 * hw coordination is in use
597 */
598static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
599{
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700600 return show_cpus(policy->related_cpus, buf);
601}
602
603/**
604 * show_affected_cpus - show the CPUs affected by each transition
605 */
606static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
607{
608 return show_cpus(policy->cpus, buf);
609}
610
Venki Pallipadi9e769882007-10-26 10:18:21 -0700611static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500612 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700613{
614 unsigned int freq = 0;
615 unsigned int ret;
616
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700617 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700618 return -EINVAL;
619
620 ret = sscanf(buf, "%u", &freq);
621 if (ret != 1)
622 return -EINVAL;
623
624 policy->governor->store_setspeed(policy, freq);
625
626 return count;
627}
628
629static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
630{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700631 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700632 return sprintf(buf, "<unsupported>\n");
633
634 return policy->governor->show_setspeed(policy, buf);
635}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Thomas Renningere2f74f32009-11-19 12:31:01 +0100637/**
viresh kumar8bf1ac722012-10-23 01:23:33 +0200638 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100639 */
640static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
641{
642 unsigned int limit;
643 int ret;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200644 if (cpufreq_driver->bios_limit) {
645 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
Thomas Renningere2f74f32009-11-19 12:31:01 +0100646 if (!ret)
647 return sprintf(buf, "%u\n", limit);
648 }
649 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
650}
651
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200652cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
653cpufreq_freq_attr_ro(cpuinfo_min_freq);
654cpufreq_freq_attr_ro(cpuinfo_max_freq);
655cpufreq_freq_attr_ro(cpuinfo_transition_latency);
656cpufreq_freq_attr_ro(scaling_available_governors);
657cpufreq_freq_attr_ro(scaling_driver);
658cpufreq_freq_attr_ro(scaling_cur_freq);
659cpufreq_freq_attr_ro(bios_limit);
660cpufreq_freq_attr_ro(related_cpus);
661cpufreq_freq_attr_ro(affected_cpus);
662cpufreq_freq_attr_rw(scaling_min_freq);
663cpufreq_freq_attr_rw(scaling_max_freq);
664cpufreq_freq_attr_rw(scaling_governor);
665cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Dave Jones905d77c2008-03-05 14:28:32 -0500667static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 &cpuinfo_min_freq.attr,
669 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100670 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 &scaling_min_freq.attr,
672 &scaling_max_freq.attr,
673 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700674 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 &scaling_governor.attr,
676 &scaling_driver.attr,
677 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700678 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 NULL
680};
681
Thomas Renninger8aa84ad2009-07-24 15:25:05 +0200682struct kobject *cpufreq_global_kobject;
683EXPORT_SYMBOL(cpufreq_global_kobject);
684
Dave Jones29464f22009-01-18 01:37:11 -0500685#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
686#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Dave Jones29464f22009-01-18 01:37:11 -0500688static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Dave Jones905d77c2008-03-05 14:28:32 -0500690 struct cpufreq_policy *policy = to_policy(kobj);
691 struct freq_attr *fattr = to_attr(attr);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500692 ssize_t ret = -EINVAL;
Stephen Boyda9144432012-07-20 18:14:38 +0000693 policy = cpufreq_cpu_get_sysfs(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (!policy)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500695 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800696
697 if (lock_policy_rwsem_read(policy->cpu) < 0)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500698 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800699
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530700 if (fattr->show)
701 ret = fattr->show(policy, buf);
702 else
703 ret = -EIO;
704
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800705 unlock_policy_rwsem_read(policy->cpu);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500706fail:
Stephen Boyda9144432012-07-20 18:14:38 +0000707 cpufreq_cpu_put_sysfs(policy);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500708no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return ret;
710}
711
Dave Jones905d77c2008-03-05 14:28:32 -0500712static ssize_t store(struct kobject *kobj, struct attribute *attr,
713 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Dave Jones905d77c2008-03-05 14:28:32 -0500715 struct cpufreq_policy *policy = to_policy(kobj);
716 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500717 ssize_t ret = -EINVAL;
Stephen Boyda9144432012-07-20 18:14:38 +0000718 policy = cpufreq_cpu_get_sysfs(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (!policy)
Dave Jonesa07530b2008-03-05 14:22:25 -0500720 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800721
722 if (lock_policy_rwsem_write(policy->cpu) < 0)
Dave Jonesa07530b2008-03-05 14:22:25 -0500723 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800724
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530725 if (fattr->store)
726 ret = fattr->store(policy, buf, count);
727 else
728 ret = -EIO;
729
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800730 unlock_policy_rwsem_write(policy->cpu);
Dave Jonesa07530b2008-03-05 14:22:25 -0500731fail:
Stephen Boyda9144432012-07-20 18:14:38 +0000732 cpufreq_cpu_put_sysfs(policy);
Dave Jonesa07530b2008-03-05 14:22:25 -0500733no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return ret;
735}
736
Dave Jones905d77c2008-03-05 14:28:32 -0500737static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
Dave Jones905d77c2008-03-05 14:28:32 -0500739 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200740 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 complete(&policy->kobj_unregister);
742}
743
Emese Revfy52cf25d2010-01-19 02:58:23 +0100744static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 .show = show,
746 .store = store,
747};
748
749static struct kobj_type ktype_cpufreq = {
750 .sysfs_ops = &sysfs_ops,
751 .default_attrs = default_attrs,
752 .release = cpufreq_sysfs_release,
753};
754
Dave Jones19d6f7e2009-07-08 17:35:39 -0400755/* symlink affected CPUs */
Alex Chiangcf3289d02009-11-17 20:27:08 -0700756static int cpufreq_add_dev_symlink(unsigned int cpu,
757 struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400758{
759 unsigned int j;
760 int ret = 0;
761
762 for_each_cpu(j, policy->cpus) {
763 struct cpufreq_policy *managed_policy;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800764 struct device *cpu_dev;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400765
766 if (j == cpu)
767 continue;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400768
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200769 pr_debug("CPU %u already managed, adding link\n", j);
Dave Jones19d6f7e2009-07-08 17:35:39 -0400770 managed_policy = cpufreq_cpu_get(cpu);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800771 cpu_dev = get_cpu_device(j);
772 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Dave Jones19d6f7e2009-07-08 17:35:39 -0400773 "cpufreq");
774 if (ret) {
775 cpufreq_cpu_put(managed_policy);
776 return ret;
777 }
778 }
779 return ret;
780}
781
Alex Chiangcf3289d02009-11-17 20:27:08 -0700782static int cpufreq_add_dev_interface(unsigned int cpu,
783 struct cpufreq_policy *policy,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800784 struct device *dev)
Dave Jones909a6942009-07-08 18:05:42 -0400785{
Dave Jonesecf7e462009-07-08 18:48:47 -0400786 struct cpufreq_policy new_policy;
Dave Jones909a6942009-07-08 18:05:42 -0400787 struct freq_attr **drv_attr;
788 unsigned long flags;
789 int ret = 0;
790 unsigned int j;
791
792 /* prepare interface data */
793 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800794 &dev->kobj, "cpufreq");
Dave Jones909a6942009-07-08 18:05:42 -0400795 if (ret)
796 return ret;
797
798 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200799 drv_attr = cpufreq_driver->attr;
Dave Jones909a6942009-07-08 18:05:42 -0400800 while ((drv_attr) && (*drv_attr)) {
801 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
802 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200803 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400804 drv_attr++;
805 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200806 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -0400807 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
808 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200809 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400810 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200811 if (cpufreq_driver->target) {
Dave Jones909a6942009-07-08 18:05:42 -0400812 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
813 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200814 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400815 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200816 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +0100817 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
818 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200819 goto err_out_kobj_put;
Thomas Renningere2f74f32009-11-19 12:31:01 +0100820 }
Dave Jones909a6942009-07-08 18:05:42 -0400821
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000822 write_lock_irqsave(&cpufreq_driver_lock, flags);
Dave Jones909a6942009-07-08 18:05:42 -0400823 for_each_cpu(j, policy->cpus) {
Dave Jones909a6942009-07-08 18:05:42 -0400824 per_cpu(cpufreq_cpu_data, j) = policy;
Tejun Heof1625062009-10-29 22:34:13 +0900825 per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
Dave Jones909a6942009-07-08 18:05:42 -0400826 }
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000827 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones909a6942009-07-08 18:05:42 -0400828
829 ret = cpufreq_add_dev_symlink(cpu, policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400830 if (ret)
831 goto err_out_kobj_put;
832
833 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
834 /* assure that the starting sequence is run in __cpufreq_set_policy */
835 policy->governor = NULL;
836
837 /* set default policy */
838 ret = __cpufreq_set_policy(policy, &new_policy);
839 policy->user_policy.policy = policy->policy;
840 policy->user_policy.governor = policy->governor;
841
842 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200843 pr_debug("setting policy failed\n");
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200844 if (cpufreq_driver->exit)
845 cpufreq_driver->exit(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400846 }
Dave Jones909a6942009-07-08 18:05:42 -0400847 return ret;
848
849err_out_kobj_put:
850 kobject_put(&policy->kobj);
851 wait_for_completion(&policy->kobj_unregister);
852 return ret;
853}
854
Viresh Kumarfcf80582013-01-29 14:39:08 +0000855#ifdef CONFIG_HOTPLUG_CPU
856static int cpufreq_add_policy_cpu(unsigned int cpu, unsigned int sibling,
857 struct device *dev)
858{
859 struct cpufreq_policy *policy;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200860 int ret = 0, has_target = !!cpufreq_driver->target;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000861 unsigned long flags;
862
863 policy = cpufreq_cpu_get(sibling);
864 WARN_ON(!policy);
865
Viresh Kumar820c6ca2013-04-22 00:48:03 +0200866 if (has_target)
867 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000868
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530869 lock_policy_rwsem_write(sibling);
870
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000871 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530872
Viresh Kumarfcf80582013-01-29 14:39:08 +0000873 cpumask_set_cpu(cpu, policy->cpus);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530874 per_cpu(cpufreq_policy_cpu, cpu) = policy->cpu;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000875 per_cpu(cpufreq_cpu_data, cpu) = policy;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000876 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000877
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530878 unlock_policy_rwsem_write(sibling);
879
Viresh Kumar820c6ca2013-04-22 00:48:03 +0200880 if (has_target) {
881 __cpufreq_governor(policy, CPUFREQ_GOV_START);
882 __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
883 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000884
Viresh Kumarfcf80582013-01-29 14:39:08 +0000885 ret = sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
886 if (ret) {
887 cpufreq_cpu_put(policy);
888 return ret;
889 }
890
891 return 0;
892}
893#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895/**
896 * cpufreq_add_dev - add a CPU device
897 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500898 * Adds the cpufreq interface for a CPU device.
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400899 *
900 * The Oracle says: try running cpufreq registration/unregistration concurrently
901 * with with cpu hotplugging and all hell will break loose. Tried to clean this
902 * mess up, but more thorough testing is needed. - Mathieu
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800904static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Viresh Kumarfcf80582013-01-29 14:39:08 +0000906 unsigned int j, cpu = dev->id;
Viresh Kumar65922462013-02-07 10:56:03 +0530907 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 unsigned long flags;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500910#ifdef CONFIG_HOTPLUG_CPU
Viresh Kumarfcf80582013-01-29 14:39:08 +0000911 struct cpufreq_governor *gov;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500912 int sibling;
913#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Ashok Rajc32b6b82005-10-30 14:59:54 -0800915 if (cpu_is_offline(cpu))
916 return 0;
917
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200918 pr_debug("adding CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920#ifdef CONFIG_SMP
921 /* check whether a different CPU already registered this
922 * CPU because it is in the same boat. */
923 policy = cpufreq_cpu_get(cpu);
924 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -0500925 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return 0;
927 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000928
929#ifdef CONFIG_HOTPLUG_CPU
930 /* Check if this cpu was hot-unplugged earlier and has siblings */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000931 read_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000932 for_each_online_cpu(sibling) {
933 struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530934 if (cp && cpumask_test_cpu(cpu, cp->related_cpus)) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000935 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000936 return cpufreq_add_policy_cpu(cpu, sibling, dev);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530937 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000938 }
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000939 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000940#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941#endif
942
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200943 if (!try_module_get(cpufreq_driver->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 ret = -EINVAL;
945 goto module_out;
946 }
947
Dave Jonese98df502005-10-20 15:17:43 -0700948 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
Dave Jones059019a2009-07-08 16:30:03 -0400949 if (!policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 goto nomem_out;
Dave Jones059019a2009-07-08 16:30:03 -0400951
952 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400953 goto err_free_policy;
Dave Jones059019a2009-07-08 16:30:03 -0400954
955 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400956 goto err_free_cpumask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 policy->cpu = cpu;
Viresh Kumar65922462013-02-07 10:56:03 +0530959 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Rusty Russell835481d2009-01-04 05:18:06 -0800960 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800962 /* Initially set CPU itself as the policy_cpu */
Tejun Heof1625062009-10-29 22:34:13 +0900963 per_cpu(cpufreq_policy_cpu, cpu) = cpu;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 init_completion(&policy->kobj_unregister);
David Howells65f27f32006-11-22 14:55:48 +0000966 INIT_WORK(&policy->update, handle_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 /* call driver. From then on the cpufreq must be able
969 * to accept all calls to ->verify and ->setpolicy for this CPU
970 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200971 ret = cpufreq_driver->init(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200973 pr_debug("initialization failed\n");
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530974 goto err_set_policy_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
Viresh Kumar643ae6e2013-01-12 05:14:38 +0000976
Viresh Kumarfcf80582013-01-29 14:39:08 +0000977 /* related cpus should atleast have policy->cpus */
978 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
979
Viresh Kumar643ae6e2013-01-12 05:14:38 +0000980 /*
981 * affected cpus must always be the one, which are online. We aren't
982 * managing offline cpus here.
983 */
984 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
985
Mike Chan187d9f42008-12-04 12:19:17 -0800986 policy->user_policy.min = policy->min;
987 policy->user_policy.max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Thomas Renningera1531ac2008-07-29 22:32:58 -0700989 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
990 CPUFREQ_START, policy);
991
Viresh Kumarfcf80582013-01-29 14:39:08 +0000992#ifdef CONFIG_HOTPLUG_CPU
993 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
994 if (gov) {
995 policy->governor = gov;
996 pr_debug("Restoring governor %s for cpu %d\n",
997 policy->governor->name, cpu);
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200998 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000999#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001001 ret = cpufreq_add_dev_interface(cpu, policy, dev);
Dave Jones19d6f7e2009-07-08 17:35:39 -04001002 if (ret)
1003 goto err_out_unregister;
Dave Jones8ff69732006-03-05 03:37:23 -05001004
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001005 kobject_uevent(&policy->kobj, KOBJ_ADD);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001006 module_put(cpufreq_driver->owner);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001007 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return 0;
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011err_out_unregister:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001012 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rusty Russell835481d2009-01-04 05:18:06 -08001013 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -07001014 per_cpu(cpufreq_cpu_data, j) = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001015 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -08001017 kobject_put(&policy->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 wait_for_completion(&policy->kobj_unregister);
1019
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301020err_set_policy_cpu:
1021 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Xiaotian Fengcad70a62010-07-20 20:11:02 +08001022 free_cpumask_var(policy->related_cpus);
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001023err_free_cpumask:
1024 free_cpumask_var(policy->cpus);
1025err_free_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 kfree(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027nomem_out:
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001028 module_put(cpufreq_driver->owner);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001029module_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return ret;
1031}
1032
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001033static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
1034{
1035 int j;
1036
1037 policy->last_cpu = policy->cpu;
1038 policy->cpu = cpu;
1039
Viresh Kumar3361b7b2013-02-04 11:38:51 +00001040 for_each_cpu(j, policy->cpus)
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001041 per_cpu(cpufreq_policy_cpu, j) = cpu;
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001042
1043#ifdef CONFIG_CPU_FREQ_TABLE
1044 cpufreq_frequency_table_update_policy_cpu(policy);
1045#endif
1046 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1047 CPUFREQ_UPDATE_POLICY_CPU, policy);
1048}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050/**
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001051 * __cpufreq_remove_dev - remove a CPU device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 *
1053 * Removes the cpufreq interface for a CPU device.
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001054 * Caller should already have policy_rwsem in write mode for this CPU.
1055 * This routine frees the rwsem before returning.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001057static int __cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001059 unsigned int cpu = dev->id, ret, cpus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 unsigned long flags;
1061 struct cpufreq_policy *data;
Amerigo Wang499bca92010-03-04 03:23:46 -05001062 struct kobject *kobj;
1063 struct completion *cmp;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001064 struct device *cpu_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001066 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001068 write_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 data = per_cpu(cpufreq_cpu_data, cpu);
Mike Travis7a6aedf2008-03-25 15:06:53 -07001071 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001073 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (!data) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001076 pr_debug("%s: No cpu_data found\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001080 if (cpufreq_driver->target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001082
Jacob Shin27ecddc2011-04-27 13:32:11 -05001083#ifdef CONFIG_HOTPLUG_CPU
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001084 if (!cpufreq_driver->setpolicy)
Dirk Brandewiefa69e332013-02-06 09:02:11 -08001085 strncpy(per_cpu(cpufreq_cpu_governor, cpu),
1086 data->governor->name, CPUFREQ_NAME_LEN);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001087#endif
1088
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301089 WARN_ON(lock_policy_rwsem_write(cpu));
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001090 cpus = cpumask_weight(data->cpus);
Viresh Kumare4969eb2013-04-11 08:04:53 +00001091
1092 if (cpus > 1)
1093 cpumask_clear_cpu(cpu, data->cpus);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301094 unlock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Viresh Kumar73bf0fc2013-02-05 22:21:14 +01001096 if (cpu != data->cpu) {
1097 sysfs_remove_link(&dev->kobj, "cpufreq");
1098 } else if (cpus > 1) {
Venki Pallipadiec282972007-03-26 12:03:19 -07001099 /* first sibling now owns the new sysfs dir */
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001100 cpu_dev = get_cpu_device(cpumask_first(data->cpus));
1101 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
1102 ret = kobject_move(&data->kobj, &cpu_dev->kobj);
1103 if (ret) {
1104 pr_err("%s: Failed to move kobj: %d", __func__, ret);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301105
1106 WARN_ON(lock_policy_rwsem_write(cpu));
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001107 cpumask_set_cpu(cpu, data->cpus);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301108
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001109 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301110 per_cpu(cpufreq_cpu_data, cpu) = data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001111 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301112
1113 unlock_policy_rwsem_write(cpu);
1114
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001115 ret = sysfs_create_link(&cpu_dev->kobj, &data->kobj,
1116 "cpufreq");
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001117 return -EINVAL;
1118 }
Venki Pallipadiec282972007-03-26 12:03:19 -07001119
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301120 WARN_ON(lock_policy_rwsem_write(cpu));
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001121 update_policy_cpu(data, cpu_dev->id);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301122 unlock_policy_rwsem_write(cpu);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001123 pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
1124 __func__, cpu_dev->id, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001125 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001126
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001127 /* If cpu is last user of policy, free policy */
1128 if (cpus == 1) {
Rafael J. Wysockie9ef4412013-07-30 00:32:00 +02001129 if (cpufreq_driver->target)
1130 __cpufreq_governor(data, CPUFREQ_GOV_POLICY_EXIT);
1131
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301132 lock_policy_rwsem_read(cpu);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001133 kobj = &data->kobj;
1134 cmp = &data->kobj_unregister;
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301135 unlock_policy_rwsem_read(cpu);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001136 kobject_put(kobj);
1137
1138 /* we need to make sure that the underlying kobj is actually
1139 * not referenced anymore by anybody before we proceed with
1140 * unloading.
1141 */
1142 pr_debug("waiting for dropping of refcount\n");
1143 wait_for_completion(cmp);
1144 pr_debug("wait complete\n");
1145
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001146 if (cpufreq_driver->exit)
1147 cpufreq_driver->exit(data);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001148
1149 free_cpumask_var(data->related_cpus);
1150 free_cpumask_var(data->cpus);
1151 kfree(data);
Rafael J. Wysockie9ef4412013-07-30 00:32:00 +02001152 } else {
1153 pr_debug("%s: removing link, cpu: %d\n", __func__, cpu);
1154 cpufreq_cpu_put(data);
1155 if (cpufreq_driver->target) {
1156 __cpufreq_governor(data, CPUFREQ_GOV_START);
1157 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1158 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301161 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return 0;
1163}
1164
1165
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001166static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001167{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001168 unsigned int cpu = dev->id;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001169 int retval;
1170
1171 if (cpu_is_offline(cpu))
1172 return 0;
1173
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001174 retval = __cpufreq_remove_dev(dev, sif);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001175 return retval;
1176}
1177
1178
David Howells65f27f32006-11-22 14:55:48 +00001179static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
David Howells65f27f32006-11-22 14:55:48 +00001181 struct cpufreq_policy *policy =
1182 container_of(work, struct cpufreq_policy, update);
1183 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001184 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 cpufreq_update_policy(cpu);
1186}
1187
1188/**
1189 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
1190 * @cpu: cpu number
1191 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1192 * @new_freq: CPU frequency the CPU actually runs at
1193 *
Dave Jones29464f22009-01-18 01:37:11 -05001194 * We adjust to current frequency first, and need to clean up later.
1195 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301197static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1198 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301200 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301202 unsigned long flags;
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001205 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 freqs.old = old_freq;
1209 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301210
1211 read_lock_irqsave(&cpufreq_driver_lock, flags);
1212 policy = per_cpu(cpufreq_cpu_data, cpu);
1213 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1214
1215 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1216 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217}
1218
1219
Dave Jones32ee8c32006-02-28 00:43:23 -05001220/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301221 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001222 * @cpu: CPU number
1223 *
1224 * This is the last known freq, without actually getting it from the driver.
1225 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1226 */
1227unsigned int cpufreq_quick_get(unsigned int cpu)
1228{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001229 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301230 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001231
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001232 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1233 return cpufreq_driver->get(cpu);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001234
1235 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001236 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301237 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001238 cpufreq_cpu_put(policy);
1239 }
1240
Dave Jones4d34a672008-02-07 16:33:49 -05001241 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001242}
1243EXPORT_SYMBOL(cpufreq_quick_get);
1244
Jesse Barnes3d737102011-06-28 10:59:12 -07001245/**
1246 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1247 * @cpu: CPU number
1248 *
1249 * Just return the max possible frequency for a given CPU.
1250 */
1251unsigned int cpufreq_quick_get_max(unsigned int cpu)
1252{
1253 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1254 unsigned int ret_freq = 0;
1255
1256 if (policy) {
1257 ret_freq = policy->max;
1258 cpufreq_cpu_put(policy);
1259 }
1260
1261 return ret_freq;
1262}
1263EXPORT_SYMBOL(cpufreq_quick_get_max);
1264
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001265
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001266static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001268 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301269 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001271 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001272 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001274 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301276 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001277 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301278 /* verify no discrepancy between actual and
1279 saved value exists */
1280 if (unlikely(ret_freq != policy->cur)) {
1281 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 schedule_work(&policy->update);
1283 }
1284 }
1285
Dave Jones4d34a672008-02-07 16:33:49 -05001286 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001287}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001289/**
1290 * cpufreq_get - get the current CPU frequency (in kHz)
1291 * @cpu: CPU number
1292 *
1293 * Get the CPU current (static) CPU frequency
1294 */
1295unsigned int cpufreq_get(unsigned int cpu)
1296{
1297 unsigned int ret_freq = 0;
1298 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1299
1300 if (!policy)
1301 goto out;
1302
1303 if (unlikely(lock_policy_rwsem_read(cpu)))
1304 goto out_policy;
1305
1306 ret_freq = __cpufreq_get(cpu);
1307
1308 unlock_policy_rwsem_read(cpu);
1309
1310out_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 cpufreq_cpu_put(policy);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001312out:
Dave Jones4d34a672008-02-07 16:33:49 -05001313 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314}
1315EXPORT_SYMBOL(cpufreq_get);
1316
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001317static struct subsys_interface cpufreq_interface = {
1318 .name = "cpufreq",
1319 .subsys = &cpu_subsys,
1320 .add_dev = cpufreq_add_dev,
1321 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001322};
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325/**
Mark Browndb295c82015-03-09 12:26:45 +00001326 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001327 *
Mark Browndb295c82015-03-09 12:26:45 +00001328 * This function is only executed for the boot processor. The other CPUs
1329 * have been put offline by means of CPU hotplug.
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001330 */
Mark Browndb295c82015-03-09 12:26:45 +00001331static int cpufreq_bp_suspend(void)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001332{
Mark Browndb295c82015-03-09 12:26:45 +00001333 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001334
Mark Browndb295c82015-03-09 12:26:45 +00001335 int cpu = smp_processor_id();
1336 struct cpufreq_policy *cpu_policy;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001337
Mark Browndb295c82015-03-09 12:26:45 +00001338 pr_debug("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001339
Mark Browndb295c82015-03-09 12:26:45 +00001340 /* If there's no policy for the boot CPU, we have nothing to do. */
1341 cpu_policy = cpufreq_cpu_get(cpu);
1342 if (!cpu_policy)
1343 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001344
Mark Browndb295c82015-03-09 12:26:45 +00001345 if (cpufreq_driver->suspend) {
1346 ret = cpufreq_driver->suspend(cpu_policy);
1347 if (ret)
1348 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1349 "step on CPU %u\n", cpu_policy->cpu);
Mark Brownd255fca2015-03-09 12:25:38 +00001350 }
Mark Brownadb66502014-04-18 00:48:32 +01001351
Mark Browndb295c82015-03-09 12:26:45 +00001352 cpufreq_cpu_put(cpu_policy);
1353 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001354}
1355
1356/**
Mark Browndb295c82015-03-09 12:26:45 +00001357 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 *
Mark Browndb295c82015-03-09 12:26:45 +00001359 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1360 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1361 * restored. It will verify that the current freq is in sync with
1362 * what we believe it to be. This is a bit later than when it
1363 * should be, but nonethteless it's better than calling
1364 * cpufreq_driver->get() here which might re-enable interrupts...
1365 *
1366 * This function is only executed for the boot CPU. The other CPUs have not
1367 * been turned on yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 */
Mark Browndb295c82015-03-09 12:26:45 +00001369static void cpufreq_bp_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
Mark Browndb295c82015-03-09 12:26:45 +00001371 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001372
Mark Browndb295c82015-03-09 12:26:45 +00001373 int cpu = smp_processor_id();
1374 struct cpufreq_policy *cpu_policy;
1375
1376 pr_debug("resuming cpu %u\n", cpu);
1377
1378 /* If there's no policy for the boot CPU, we have nothing to do. */
1379 cpu_policy = cpufreq_cpu_get(cpu);
1380 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001381 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Mark Browndb295c82015-03-09 12:26:45 +00001383 if (cpufreq_driver->resume) {
1384 ret = cpufreq_driver->resume(cpu_policy);
1385 if (ret) {
1386 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1387 "step on CPU %u\n", cpu_policy->cpu);
1388 goto fail;
1389 }
Mark Brownd255fca2015-03-09 12:25:38 +00001390 }
Mark Brownadb66502014-04-18 00:48:32 +01001391
Mark Browndb295c82015-03-09 12:26:45 +00001392 schedule_work(&cpu_policy->update);
Mark Brownadb66502014-04-18 00:48:32 +01001393
Mark Browndb295c82015-03-09 12:26:45 +00001394fail:
1395 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396}
1397
Mark Browndb295c82015-03-09 12:26:45 +00001398static struct syscore_ops cpufreq_syscore_ops = {
1399 .suspend = cpufreq_bp_suspend,
1400 .resume = cpufreq_bp_resume,
1401};
1402
Borislav Petkov9d950462013-01-20 10:24:28 +00001403/**
1404 * cpufreq_get_current_driver - return current driver's name
1405 *
1406 * Return the name string of the currently loaded cpufreq driver
1407 * or NULL, if none.
1408 */
1409const char *cpufreq_get_current_driver(void)
1410{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001411 if (cpufreq_driver)
1412 return cpufreq_driver->name;
1413
1414 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001415}
1416EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418/*********************************************************************
1419 * NOTIFIER LISTS INTERFACE *
1420 *********************************************************************/
1421
1422/**
1423 * cpufreq_register_notifier - register a driver with cpufreq
1424 * @nb: notifier function to register
1425 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1426 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001427 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 * are notified about clock rate changes (once before and once after
1429 * the transition), or a list of drivers that are notified about
1430 * changes in cpufreq policy.
1431 *
1432 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001433 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 */
1435int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1436{
1437 int ret;
1438
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001439 if (cpufreq_disabled())
1440 return -EINVAL;
1441
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001442 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 switch (list) {
1445 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001446 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001447 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 break;
1449 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001450 ret = blocking_notifier_chain_register(
1451 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 break;
1453 default:
1454 ret = -EINVAL;
1455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 return ret;
1458}
1459EXPORT_SYMBOL(cpufreq_register_notifier);
1460
1461
1462/**
1463 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1464 * @nb: notifier block to be unregistered
1465 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1466 *
1467 * Remove a driver from the CPU frequency notifier list.
1468 *
1469 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001470 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 */
1472int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1473{
1474 int ret;
1475
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001476 if (cpufreq_disabled())
1477 return -EINVAL;
1478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 switch (list) {
1480 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001481 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001482 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 break;
1484 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001485 ret = blocking_notifier_chain_unregister(
1486 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 break;
1488 default:
1489 ret = -EINVAL;
1490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 return ret;
1493}
1494EXPORT_SYMBOL(cpufreq_unregister_notifier);
1495
1496
1497/*********************************************************************
1498 * GOVERNORS *
1499 *********************************************************************/
1500
1501
1502int __cpufreq_driver_target(struct cpufreq_policy *policy,
1503 unsigned int target_freq,
1504 unsigned int relation)
1505{
1506 int retval = -EINVAL;
Viresh Kumar72499242012-10-31 01:28:21 +01001507 unsigned int old_target_freq = target_freq;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001508
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001509 if (cpufreq_disabled())
1510 return -ENODEV;
1511
Viresh Kumar72499242012-10-31 01:28:21 +01001512 /* Make sure that target_freq is within supported range */
1513 if (target_freq > policy->max)
1514 target_freq = policy->max;
1515 if (target_freq < policy->min)
1516 target_freq = policy->min;
1517
1518 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1519 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001520
1521 if (target_freq == policy->cur)
1522 return 0;
1523
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001524 if (cpufreq_driver->target)
1525 retval = cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 return retval;
1528}
1529EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531int cpufreq_driver_target(struct cpufreq_policy *policy,
1532 unsigned int target_freq,
1533 unsigned int relation)
1534{
Julia Lawallf1829e42008-07-25 22:44:53 +02001535 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537 policy = cpufreq_cpu_get(policy->cpu);
1538 if (!policy)
Julia Lawallf1829e42008-07-25 22:44:53 +02001539 goto no_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001541 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
Julia Lawallf1829e42008-07-25 22:44:53 +02001542 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544 ret = __cpufreq_driver_target(policy, target_freq, relation);
1545
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001546 unlock_policy_rwsem_write(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Julia Lawallf1829e42008-07-25 22:44:53 +02001548fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 cpufreq_cpu_put(policy);
Julia Lawallf1829e42008-07-25 22:44:53 +02001550no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 return ret;
1552}
1553EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1554
venkatesh.pallipadi@intel.combf0b90e2008-08-04 11:59:07 -07001555int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu)
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001556{
1557 int ret = 0;
1558
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001559 if (cpufreq_disabled())
1560 return ret;
1561
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001562 if (!cpufreq_driver->getavg)
Viresh Kumar0676f7f2012-10-24 23:39:48 +02001563 return 0;
1564
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001565 policy = cpufreq_cpu_get(policy->cpu);
1566 if (!policy)
1567 return -EINVAL;
1568
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001569 ret = cpufreq_driver->getavg(policy, cpu);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001570
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001571 cpufreq_cpu_put(policy);
1572 return ret;
1573}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001574EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001575
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001576/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001577 * when "event" is CPUFREQ_GOV_LIMITS
1578 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301580static int __cpufreq_governor(struct cpufreq_policy *policy,
1581 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
Dave Jonescc993ca2005-07-28 09:43:56 -07001583 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001584
1585 /* Only must be defined when default governor is known to have latency
1586 restrictions, like e.g. conservative or ondemand.
1587 That this is the case is already ensured in Kconfig
1588 */
1589#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1590 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1591#else
1592 struct cpufreq_governor *gov = NULL;
1593#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001594
1595 if (policy->governor->max_transition_latency &&
1596 policy->cpuinfo.transition_latency >
1597 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07001598 if (!gov)
1599 return -EINVAL;
1600 else {
1601 printk(KERN_WARNING "%s governor failed, too long"
1602 " transition latency of HW, fallback"
1603 " to %s governor\n",
1604 policy->governor->name,
1605 gov->name);
1606 policy->governor = gov;
1607 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
1610 if (!try_module_get(policy->governor->owner))
1611 return -EINVAL;
1612
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001613 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301614 policy->cpu, event);
Xiaoguang Chenba17ca42013-06-19 15:00:07 +08001615
1616 mutex_lock(&cpufreq_governor_lock);
1617 if ((!policy->governor_enabled && (event == CPUFREQ_GOV_STOP)) ||
1618 (policy->governor_enabled && (event == CPUFREQ_GOV_START))) {
1619 mutex_unlock(&cpufreq_governor_lock);
1620 return -EBUSY;
1621 }
1622
1623 if (event == CPUFREQ_GOV_STOP)
1624 policy->governor_enabled = false;
1625 else if (event == CPUFREQ_GOV_START)
1626 policy->governor_enabled = true;
1627
1628 mutex_unlock(&cpufreq_governor_lock);
1629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 ret = policy->governor->governor(policy, event);
1631
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001632 if (!ret) {
1633 if (event == CPUFREQ_GOV_POLICY_INIT)
1634 policy->governor->initialized++;
1635 else if (event == CPUFREQ_GOV_POLICY_EXIT)
1636 policy->governor->initialized--;
Xiaoguang Chenba17ca42013-06-19 15:00:07 +08001637 } else {
1638 /* Restore original values */
1639 mutex_lock(&cpufreq_governor_lock);
1640 if (event == CPUFREQ_GOV_STOP)
1641 policy->governor_enabled = true;
1642 else if (event == CPUFREQ_GOV_START)
1643 policy->governor_enabled = false;
1644 mutex_unlock(&cpufreq_governor_lock);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001645 }
Viresh Kumarb3940582013-02-01 05:42:58 +00001646
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301647 /* we keep one module reference alive for
1648 each CPU governed by this CPU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 if ((event != CPUFREQ_GOV_START) || ret)
1650 module_put(policy->governor->owner);
1651 if ((event == CPUFREQ_GOV_STOP) && !ret)
1652 module_put(policy->governor->owner);
1653
1654 return ret;
1655}
1656
1657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658int cpufreq_register_governor(struct cpufreq_governor *governor)
1659{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001660 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
1662 if (!governor)
1663 return -EINVAL;
1664
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001665 if (cpufreq_disabled())
1666 return -ENODEV;
1667
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001668 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001669
Viresh Kumarb3940582013-02-01 05:42:58 +00001670 governor->initialized = 0;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001671 err = -EBUSY;
1672 if (__find_governor(governor->name) == NULL) {
1673 err = 0;
1674 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Dave Jones32ee8c32006-02-28 00:43:23 -05001677 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001678 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679}
1680EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1681
1682
1683void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1684{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001685#ifdef CONFIG_HOTPLUG_CPU
1686 int cpu;
1687#endif
1688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 if (!governor)
1690 return;
1691
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001692 if (cpufreq_disabled())
1693 return;
1694
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001695#ifdef CONFIG_HOTPLUG_CPU
1696 for_each_present_cpu(cpu) {
1697 if (cpu_online(cpu))
1698 continue;
1699 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1700 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1701 }
1702#endif
1703
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001704 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001706 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 return;
1708}
1709EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1710
1711
1712
1713/*********************************************************************
1714 * POLICY INTERFACE *
1715 *********************************************************************/
1716
1717/**
1718 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05001719 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1720 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 *
1722 * Reads the current cpufreq policy.
1723 */
1724int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1725{
1726 struct cpufreq_policy *cpu_policy;
1727 if (!policy)
1728 return -EINVAL;
1729
1730 cpu_policy = cpufreq_cpu_get(cpu);
1731 if (!cpu_policy)
1732 return -EINVAL;
1733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
1736 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 return 0;
1738}
1739EXPORT_SYMBOL(cpufreq_get_policy);
1740
1741
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001742/*
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301743 * data : current policy.
1744 * policy : policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001745 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301746static int __cpufreq_set_policy(struct cpufreq_policy *data,
1747 struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748{
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001749 int ret = 0, failed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001751 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 policy->min, policy->max);
1753
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301754 memcpy(&policy->cpuinfo, &data->cpuinfo,
1755 sizeof(struct cpufreq_cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Yi Yang53391fa2008-01-30 13:33:34 +01001757 if (policy->min > data->max || policy->max < data->min) {
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02001758 ret = -EINVAL;
1759 goto error_out;
1760 }
1761
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 /* verify the cpu speed can be set within this limit */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001763 ret = cpufreq_driver->verify(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 if (ret)
1765 goto error_out;
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001768 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1769 CPUFREQ_ADJUST, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001772 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1773 CPUFREQ_INCOMPATIBLE, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
1775 /* verify the cpu speed can be set within this limit,
1776 which might be different to the first one */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001777 ret = cpufreq_driver->verify(policy);
Alan Sterne041c682006-03-27 01:16:30 -08001778 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001782 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1783 CPUFREQ_NOTIFY, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Dave Jones7d5e3502006-02-02 17:03:42 -05001785 data->min = policy->min;
1786 data->max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001788 pr_debug("new min and max freqs are %u - %u kHz\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301789 data->min, data->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001791 if (cpufreq_driver->setpolicy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 data->policy = policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001793 pr_debug("setting range\n");
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001794 ret = cpufreq_driver->setpolicy(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 } else {
1796 if (policy->governor != data->governor) {
1797 /* save old, working values */
1798 struct cpufreq_governor *old_gov = data->governor;
1799
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001800 pr_debug("governor switch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
1802 /* end old governor */
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001803 if (data->governor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Viresh Kumar955ef482013-05-16 05:09:58 +00001805 unlock_policy_rwsem_write(policy->cpu);
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001806 __cpufreq_governor(data,
1807 CPUFREQ_GOV_POLICY_EXIT);
Viresh Kumar955ef482013-05-16 05:09:58 +00001808 lock_policy_rwsem_write(policy->cpu);
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
1811 /* start new governor */
1812 data->governor = policy->governor;
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001813 if (!__cpufreq_governor(data, CPUFREQ_GOV_POLICY_INIT)) {
Viresh Kumar955ef482013-05-16 05:09:58 +00001814 if (!__cpufreq_governor(data, CPUFREQ_GOV_START)) {
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001815 failed = 0;
Viresh Kumar955ef482013-05-16 05:09:58 +00001816 } else {
1817 unlock_policy_rwsem_write(policy->cpu);
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001818 __cpufreq_governor(data,
1819 CPUFREQ_GOV_POLICY_EXIT);
Viresh Kumar955ef482013-05-16 05:09:58 +00001820 lock_policy_rwsem_write(policy->cpu);
1821 }
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001822 }
1823
1824 if (failed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 /* new governor failed, so re-start old one */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001826 pr_debug("starting governor %s failed\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301827 data->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 if (old_gov) {
1829 data->governor = old_gov;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301830 __cpufreq_governor(data,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001831 CPUFREQ_GOV_POLICY_INIT);
1832 __cpufreq_governor(data,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301833 CPUFREQ_GOV_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 }
1835 ret = -EINVAL;
1836 goto error_out;
1837 }
1838 /* might be a policy change, too, so fall through */
1839 }
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001840 pr_debug("governor: change or update limits\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1842 }
1843
Dave Jones7d5e3502006-02-02 17:03:42 -05001844error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 return ret;
1846}
1847
1848/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1850 * @cpu: CPU which shall be re-evaluated
1851 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001852 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 * at different times.
1854 */
1855int cpufreq_update_policy(unsigned int cpu)
1856{
1857 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1858 struct cpufreq_policy policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02001859 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Julia Lawallf1829e42008-07-25 22:44:53 +02001861 if (!data) {
1862 ret = -ENODEV;
1863 goto no_policy;
1864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Julia Lawallf1829e42008-07-25 22:44:53 +02001866 if (unlikely(lock_policy_rwsem_write(cpu))) {
1867 ret = -EINVAL;
1868 goto fail;
1869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001871 pr_debug("updating policy for CPU %u\n", cpu);
Dave Jones7d5e3502006-02-02 17:03:42 -05001872 memcpy(&policy, data, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 policy.min = data->user_policy.min;
1874 policy.max = data->user_policy.max;
1875 policy.policy = data->user_policy.policy;
1876 policy.governor = data->user_policy.governor;
1877
Thomas Renninger0961dd02006-01-26 18:46:33 +01001878 /* BIOS might change freq behind our back
1879 -> ask driver for current freq and notify governors about a change */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001880 if (cpufreq_driver->get) {
1881 policy.cur = cpufreq_driver->get(cpu);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001882 if (!data->cur) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001883 pr_debug("Driver did not initialize current freq");
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001884 data->cur = policy.cur;
1885 } else {
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001886 if (data->cur != policy.cur && cpufreq_driver->target)
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301887 cpufreq_out_of_sync(cpu, data->cur,
1888 policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001889 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01001890 }
1891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 ret = __cpufreq_set_policy(data, &policy);
1893
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001894 unlock_policy_rwsem_write(cpu);
1895
Julia Lawallf1829e42008-07-25 22:44:53 +02001896fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 cpufreq_cpu_put(data);
Julia Lawallf1829e42008-07-25 22:44:53 +02001898no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 return ret;
1900}
1901EXPORT_SYMBOL(cpufreq_update_policy);
1902
Satyam Sharmadd184a02007-10-02 13:28:14 -07001903static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08001904 unsigned long action, void *hcpu)
1905{
1906 unsigned int cpu = (unsigned long)hcpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001907 struct device *dev;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001908
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001909 dev = get_cpu_device(cpu);
1910 if (dev) {
Ashok Rajc32b6b82005-10-30 14:59:54 -08001911 switch (action) {
1912 case CPU_ONLINE:
Srivatsa S. Bhat9d3ce4a2013-07-12 03:45:37 +05301913 case CPU_ONLINE_FROZEN:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001914 cpufreq_add_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001915 break;
1916 case CPU_DOWN_PREPARE:
Srivatsa S. Bhat9d3ce4a2013-07-12 03:45:37 +05301917 case CPU_DOWN_PREPARE_FROZEN:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001918 __cpufreq_remove_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001919 break;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001920 case CPU_DOWN_FAILED:
Srivatsa S. Bhat9d3ce4a2013-07-12 03:45:37 +05301921 case CPU_DOWN_FAILED_FROZEN:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001922 cpufreq_add_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001923 break;
1924 }
1925 }
1926 return NOTIFY_OK;
1927}
1928
Neal Buckendahl9c36f742010-06-22 22:02:44 -05001929static struct notifier_block __refdata cpufreq_cpu_notifier = {
Ashok Rajc32b6b82005-10-30 14:59:54 -08001930 .notifier_call = cpufreq_cpu_callback,
1931};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
1933/*********************************************************************
1934 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1935 *********************************************************************/
1936
1937/**
1938 * cpufreq_register_driver - register a CPU Frequency driver
1939 * @driver_data: A struct cpufreq_driver containing the values#
1940 * submitted by the CPU Frequency driver.
1941 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001942 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05001944 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 *
1946 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001947int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948{
1949 unsigned long flags;
1950 int ret;
1951
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001952 if (cpufreq_disabled())
1953 return -ENODEV;
1954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 if (!driver_data || !driver_data->verify || !driver_data->init ||
1956 ((!driver_data->setpolicy) && (!driver_data->target)))
1957 return -EINVAL;
1958
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001959 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
1961 if (driver_data->setpolicy)
1962 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1963
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001964 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001965 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001966 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 return -EBUSY;
1968 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001969 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001970 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001972 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001973 if (ret)
1974 goto err_null_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001976 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 int i;
1978 ret = -ENODEV;
1979
1980 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07001981 for (i = 0; i < nr_cpu_ids; i++)
1982 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07001984 break;
1985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
1987 /* if all ->init() calls failed, unregister */
1988 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001989 pr_debug("no CPU initialized for driver %s\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301990 driver_data->name);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001991 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 }
1993 }
1994
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001995 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001996 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001998 return 0;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001999err_if_unreg:
2000 subsys_interface_unregister(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002001err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002002 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002003 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002004 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05002005 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006}
2007EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2008
2009
2010/**
2011 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2012 *
Dave Jones32ee8c32006-02-28 00:43:23 -05002013 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 * the right to do so, i.e. if you have succeeded in initialising before!
2015 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2016 * currently not initialised.
2017 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002018int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019{
2020 unsigned long flags;
2021
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002022 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002025 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002027 subsys_interface_unregister(&cpufreq_interface);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07002028 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002030 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002031 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002032 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
2034 return 0;
2035}
2036EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002037
2038static int __init cpufreq_core_init(void)
2039{
2040 int cpu;
2041
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002042 if (cpufreq_disabled())
2043 return -ENODEV;
2044
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002045 for_each_possible_cpu(cpu) {
Tejun Heof1625062009-10-29 22:34:13 +09002046 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002047 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
2048 }
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002049
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002050 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002051 BUG_ON(!cpufreq_global_kobject);
Mark Browndb295c82015-03-09 12:26:45 +00002052 register_syscore_ops(&cpufreq_syscore_ops);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002053
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002054 return 0;
2055}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002056core_initcall(cpufreq_core_init);