blob: ce9273a7b4e342be629d9a28d47140f63881fd9a [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 Kumar72a4ce32013-05-17 11:26:32 +000020#include <asm/cputime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/kernel.h>
Viresh Kumar72a4ce32013-05-17 11:26:32 +000022#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 Kumar72a4ce32013-05-17 11:26:32 +000030#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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080053/*
54 * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
55 * all cpufreq/hotplug/workqueue/etc related lock issues.
56 *
57 * The rules for this semaphore:
58 * - Any routine that wants to read from the policy structure will
59 * do a down_read on this semaphore.
60 * - Any routine that will write to the policy structure and/or may take away
61 * the policy altogether (eg. CPU hotplug), will hold this lock in write
62 * mode before doing so.
63 *
64 * Additional rules:
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080065 * - Governor routines that can be called in cpufreq hotplug path should not
66 * take this sem as top level hotplug notifier handler takes this.
Mathieu Desnoyers395913d2009-06-08 13:17:31 -040067 * - Lock should not be held across
68 * __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080069 */
Tejun Heof1625062009-10-29 22:34:13 +090070static DEFINE_PER_CPU(int, cpufreq_policy_cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080071static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
72
73#define lock_policy_rwsem(mode, cpu) \
Viresh Kumarfa1d8af2013-02-07 15:38:42 +053074static int lock_policy_rwsem_##mode(int cpu) \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080075{ \
Tejun Heof1625062009-10-29 22:34:13 +090076 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080077 BUG_ON(policy_cpu == -1); \
78 down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080079 \
80 return 0; \
81}
82
83lock_policy_rwsem(read, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080084lock_policy_rwsem(write, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080085
Viresh Kumarfa1d8af2013-02-07 15:38:42 +053086#define unlock_policy_rwsem(mode, cpu) \
87static void unlock_policy_rwsem_##mode(int cpu) \
88{ \
89 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
90 BUG_ON(policy_cpu == -1); \
91 up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080092}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080093
Viresh Kumarfa1d8af2013-02-07 15:38:42 +053094unlock_policy_rwsem(read, cpu);
95unlock_policy_rwsem(write, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097/* internal prototypes */
Dave Jones29464f22009-01-18 01:37:11 -050098static int __cpufreq_governor(struct cpufreq_policy *policy,
99 unsigned int event);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800100static unsigned int __cpufreq_get(unsigned int cpu);
David Howells65f27f32006-11-22 14:55:48 +0000101static void handle_update(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103/**
Dave Jones32ee8c32006-02-28 00:43:23 -0500104 * Two notifier lists: the "policy" list is involved in the
105 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 * "transition" list for kernel code that needs to handle
107 * changes to devices when the CPU clock speed changes.
108 * The mutex locks both lists.
109 */
Alan Sterne041c682006-03-27 01:16:30 -0800110static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700111static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200113static bool init_cpufreq_transition_notifier_list_called;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700114static int __init init_cpufreq_transition_notifier_list(void)
115{
116 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200117 init_cpufreq_transition_notifier_list_called = true;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700118 return 0;
119}
Linus Torvaldsb3438f82006-11-20 11:47:18 -0800120pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400122static int off __read_mostly;
Viresh Kumarda584452012-10-26 00:51:32 +0200123static int cpufreq_disabled(void)
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400124{
125 return off;
126}
127void disable_cpufreq(void)
128{
129 off = 1;
130}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131static LIST_HEAD(cpufreq_governor_list);
Dave Jones29464f22009-01-18 01:37:11 -0500132static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000134bool have_governor_per_policy(void)
135{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200136 return cpufreq_driver->have_governor_per_policy;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000137}
Viresh Kumar3f869d62013-05-16 05:09:56 +0000138EXPORT_SYMBOL_GPL(have_governor_per_policy);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000139
Viresh Kumar944e9a02013-05-16 05:09:57 +0000140struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
141{
142 if (have_governor_per_policy())
143 return &policy->kobj;
144 else
145 return cpufreq_global_kobject;
146}
147EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
148
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000149static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
150{
151 u64 idle_time;
152 u64 cur_wall_time;
153 u64 busy_time;
154
155 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
156
157 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
158 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
159 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
160 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
161 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
162 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
163
164 idle_time = cur_wall_time - busy_time;
165 if (wall)
166 *wall = cputime_to_usecs(cur_wall_time);
167
168 return cputime_to_usecs(idle_time);
169}
170
171u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
172{
173 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
174
175 if (idle_time == -1ULL)
176 return get_cpu_idle_time_jiffy(cpu, wall);
177 else if (!io_busy)
178 idle_time += get_cpu_iowait_time_us(cpu, wall);
179
180 return idle_time;
181}
182EXPORT_SYMBOL_GPL(get_cpu_idle_time);
183
Stephen Boyda9144432012-07-20 18:14:38 +0000184static struct cpufreq_policy *__cpufreq_cpu_get(unsigned int cpu, bool sysfs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 struct cpufreq_policy *data;
187 unsigned long flags;
188
Mike Travis7a6aedf2008-03-25 15:06:53 -0700189 if (cpu >= nr_cpu_ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 goto err_out;
191
192 /* get the cpufreq driver */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000193 read_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200195 if (!cpufreq_driver)
196 goto err_out_unlock;
197
198 if (!try_module_get(cpufreq_driver->owner))
199 goto err_out_unlock;
200
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 /* get the CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -0700203 data = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 if (!data)
206 goto err_out_put_module;
207
Stephen Boyda9144432012-07-20 18:14:38 +0000208 if (!sysfs && !kobject_get(&data->kobj))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 goto err_out_put_module;
210
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000211 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return data;
213
Dave Jones7d5e3502006-02-02 17:03:42 -0500214err_out_put_module:
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200215 module_put(cpufreq_driver->owner);
Nathan Zimmer58000432013-04-04 14:53:25 +0000216err_out_unlock:
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200217 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones7d5e3502006-02-02 17:03:42 -0500218err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return NULL;
220}
Stephen Boyda9144432012-07-20 18:14:38 +0000221
222struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
223{
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000224 if (cpufreq_disabled())
225 return NULL;
226
Stephen Boyda9144432012-07-20 18:14:38 +0000227 return __cpufreq_cpu_get(cpu, false);
228}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
230
Stephen Boyda9144432012-07-20 18:14:38 +0000231static struct cpufreq_policy *cpufreq_cpu_get_sysfs(unsigned int cpu)
232{
233 return __cpufreq_cpu_get(cpu, true);
234}
235
236static void __cpufreq_cpu_put(struct cpufreq_policy *data, bool sysfs)
237{
238 if (!sysfs)
239 kobject_put(&data->kobj);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200240 module_put(cpufreq_driver->owner);
Stephen Boyda9144432012-07-20 18:14:38 +0000241}
Dave Jones7d5e3502006-02-02 17:03:42 -0500242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243void cpufreq_cpu_put(struct cpufreq_policy *data)
244{
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000245 if (cpufreq_disabled())
246 return;
247
Stephen Boyda9144432012-07-20 18:14:38 +0000248 __cpufreq_cpu_put(data, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
251
Stephen Boyda9144432012-07-20 18:14:38 +0000252static void cpufreq_cpu_put_sysfs(struct cpufreq_policy *data)
253{
254 __cpufreq_cpu_put(data, true);
255}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
259 *********************************************************************/
260
261/**
262 * adjust_jiffies - adjust the system "loops_per_jiffy"
263 *
264 * This function alters the system "loops_per_jiffy" for the clock
265 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500266 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 * per-CPU loops_per_jiffy value wherever possible.
268 */
269#ifndef CONFIG_SMP
270static unsigned long l_p_j_ref;
271static unsigned int l_p_j_ref_freq;
272
Arjan van de Ven858119e2006-01-14 13:20:43 -0800273static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 if (ci->flags & CPUFREQ_CONST_LOOPS)
276 return;
277
278 if (!l_p_j_ref_freq) {
279 l_p_j_ref = loops_per_jiffy;
280 l_p_j_ref_freq = ci->old;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200281 pr_debug("saving %lu as reference value for loops_per_jiffy; "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530282 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
Afzal Mohammedd08de0c12012-01-04 10:52:46 +0530284 if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700285 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530286 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
287 ci->new);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200288 pr_debug("scaling loops_per_jiffy to %lu "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530289 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291}
292#else
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530293static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
294{
295 return;
296}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297#endif
298
299
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530300void __cpufreq_notify_transition(struct cpufreq_policy *policy,
301 struct cpufreq_freqs *freqs, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 BUG_ON(irqs_disabled());
304
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000305 if (cpufreq_disabled())
306 return;
307
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200308 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200309 pr_debug("notification %u of frequency transition to %u kHz\n",
Dave Jonese4472cb2006-01-31 15:53:55 -0800310 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500315 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800316 * which is not equal to what the cpufreq core thinks is
317 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200319 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800320 if ((policy) && (policy->cpu == freqs->cpu) &&
321 (policy->cur) && (policy->cur != freqs->old)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200322 pr_debug("Warning: CPU frequency is"
Dave Jonese4472cb2006-01-31 15:53:55 -0800323 " %u, cpufreq assumed %u kHz.\n",
324 freqs->old, policy->cur);
325 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
327 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700328 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800329 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
331 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 case CPUFREQ_POSTCHANGE:
334 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200335 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
Thomas Renninger6f4f2722010-04-20 13:17:36 +0200336 (unsigned long)freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100337 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700338 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800339 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800340 if (likely(policy) && likely(policy->cpu == freqs->cpu))
341 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 break;
343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530345/**
346 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
347 * on frequency transition.
348 *
349 * This function calls the transition notifiers and the "adjust_jiffies"
350 * function. It is called twice on all CPU frequency changes that have
351 * external effects.
352 */
353void cpufreq_notify_transition(struct cpufreq_policy *policy,
354 struct cpufreq_freqs *freqs, unsigned int state)
355{
356 for_each_cpu(freqs->cpu, policy->cpus)
357 __cpufreq_notify_transition(policy, freqs, state);
358}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
360
361
362
363/*********************************************************************
364 * SYSFS INTERFACE *
365 *********************************************************************/
366
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700367static struct cpufreq_governor *__find_governor(const char *str_governor)
368{
369 struct cpufreq_governor *t;
370
371 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
Dave Jones29464f22009-01-18 01:37:11 -0500372 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700373 return t;
374
375 return NULL;
376}
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378/**
379 * cpufreq_parse_governor - parse a governor string
380 */
Dave Jones905d77c2008-03-05 14:28:32 -0500381static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 struct cpufreq_governor **governor)
383{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700384 int err = -EINVAL;
385
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200386 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700387 goto out;
388
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200389 if (cpufreq_driver->setpolicy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
391 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700392 err = 0;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530393 } else if (!strnicmp(str_governor, "powersave",
394 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700396 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200398 } else if (cpufreq_driver->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700400
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800401 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700402
403 t = __find_governor(str_governor);
404
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700405 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700406 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700407
Kees Cook1a8e1462011-05-04 08:38:56 -0700408 mutex_unlock(&cpufreq_governor_mutex);
409 ret = request_module("cpufreq_%s", str_governor);
410 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700411
Kees Cook1a8e1462011-05-04 08:38:56 -0700412 if (ret == 0)
413 t = __find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700414 }
415
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700416 if (t != NULL) {
417 *governor = t;
418 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700420
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800421 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
Dave Jones29464f22009-01-18 01:37:11 -0500423out:
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700424 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530429 * cpufreq_per_cpu_attr_read() / show_##file_name() -
430 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 *
432 * Write out information from cpufreq_driver->policy[cpu]; object must be
433 * "unsigned int".
434 */
435
Dave Jones32ee8c32006-02-28 00:43:23 -0500436#define show_one(file_name, object) \
437static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500438(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500439{ \
Dave Jones29464f22009-01-18 01:37:11 -0500440 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
443show_one(cpuinfo_min_freq, cpuinfo.min_freq);
444show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100445show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446show_one(scaling_min_freq, min);
447show_one(scaling_max_freq, max);
448show_one(scaling_cur_freq, cur);
449
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530450static int __cpufreq_set_policy(struct cpufreq_policy *data,
451 struct cpufreq_policy *policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453/**
454 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
455 */
456#define store_one(file_name, object) \
457static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500458(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{ \
Jingoo Hanf55c9c22012-10-31 05:49:13 +0000460 unsigned int ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 struct cpufreq_policy new_policy; \
462 \
463 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
464 if (ret) \
465 return -EINVAL; \
466 \
Dave Jones29464f22009-01-18 01:37:11 -0500467 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (ret != 1) \
469 return -EINVAL; \
470 \
Thomas Renninger7970e082006-04-13 15:14:04 +0200471 ret = __cpufreq_set_policy(policy, &new_policy); \
472 policy->user_policy.object = policy->object; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 \
474 return ret ? ret : count; \
475}
476
Dave Jones29464f22009-01-18 01:37:11 -0500477store_one(scaling_min_freq, min);
478store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480/**
481 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
482 */
Dave Jones905d77c2008-03-05 14:28:32 -0500483static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
484 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800486 unsigned int cur_freq = __cpufreq_get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (!cur_freq)
488 return sprintf(buf, "<unknown>");
489 return sprintf(buf, "%u\n", cur_freq);
490}
491
492
493/**
494 * show_scaling_governor - show the current policy for the specified CPU
495 */
Dave Jones905d77c2008-03-05 14:28:32 -0500496static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Dave Jones29464f22009-01-18 01:37:11 -0500498 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return sprintf(buf, "powersave\n");
500 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
501 return sprintf(buf, "performance\n");
502 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200503 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500504 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return -EINVAL;
506}
507
508
509/**
510 * store_scaling_governor - store policy for the specified CPU
511 */
Dave Jones905d77c2008-03-05 14:28:32 -0500512static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
513 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Jingoo Hanf55c9c22012-10-31 05:49:13 +0000515 unsigned int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 char str_governor[16];
517 struct cpufreq_policy new_policy;
518
519 ret = cpufreq_get_policy(&new_policy, policy->cpu);
520 if (ret)
521 return ret;
522
Dave Jones29464f22009-01-18 01:37:11 -0500523 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (ret != 1)
525 return -EINVAL;
526
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530527 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
528 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return -EINVAL;
530
Thomas Renninger7970e082006-04-13 15:14:04 +0200531 /* Do not use cpufreq_set_policy here or the user_policy.max
532 will be wrongly overridden */
Thomas Renninger7970e082006-04-13 15:14:04 +0200533 ret = __cpufreq_set_policy(policy, &new_policy);
534
535 policy->user_policy.policy = policy->policy;
536 policy->user_policy.governor = policy->governor;
Thomas Renninger7970e082006-04-13 15:14:04 +0200537
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530538 if (ret)
539 return ret;
540 else
541 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
544/**
545 * show_scaling_driver - show the cpufreq driver currently loaded
546 */
Dave Jones905d77c2008-03-05 14:28:32 -0500547static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200549 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
552/**
553 * show_scaling_available_governors - show the available CPUfreq governors
554 */
Dave Jones905d77c2008-03-05 14:28:32 -0500555static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
556 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 ssize_t i = 0;
559 struct cpufreq_governor *t;
560
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200561 if (!cpufreq_driver->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 i += sprintf(buf, "performance powersave");
563 goto out;
564 }
565
566 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
Dave Jones29464f22009-01-18 01:37:11 -0500567 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
568 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200570 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500572out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 i += sprintf(&buf[i], "\n");
574 return i;
575}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700576
Rusty Russell835481d2009-01-04 05:18:06 -0800577static ssize_t show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
579 ssize_t i = 0;
580 unsigned int cpu;
581
Rusty Russell835481d2009-01-04 05:18:06 -0800582 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (i)
584 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
585 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
586 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500587 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
589 i += sprintf(&buf[i], "\n");
590 return i;
591}
592
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700593/**
594 * show_related_cpus - show the CPUs affected by each transition even if
595 * hw coordination is in use
596 */
597static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
598{
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700599 return show_cpus(policy->related_cpus, buf);
600}
601
602/**
603 * show_affected_cpus - show the CPUs affected by each transition
604 */
605static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
606{
607 return show_cpus(policy->cpus, buf);
608}
609
Venki Pallipadi9e769882007-10-26 10:18:21 -0700610static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500611 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700612{
613 unsigned int freq = 0;
614 unsigned int ret;
615
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700616 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700617 return -EINVAL;
618
619 ret = sscanf(buf, "%u", &freq);
620 if (ret != 1)
621 return -EINVAL;
622
623 policy->governor->store_setspeed(policy, freq);
624
625 return count;
626}
627
628static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
629{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700630 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700631 return sprintf(buf, "<unsupported>\n");
632
633 return policy->governor->show_setspeed(policy, buf);
634}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Thomas Renningere2f74f32009-11-19 12:31:01 +0100636/**
viresh kumar8bf1ac72012-10-23 01:23:33 +0200637 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100638 */
639static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
640{
641 unsigned int limit;
642 int ret;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200643 if (cpufreq_driver->bios_limit) {
644 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
Thomas Renningere2f74f32009-11-19 12:31:01 +0100645 if (!ret)
646 return sprintf(buf, "%u\n", limit);
647 }
648 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
649}
650
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200651cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
652cpufreq_freq_attr_ro(cpuinfo_min_freq);
653cpufreq_freq_attr_ro(cpuinfo_max_freq);
654cpufreq_freq_attr_ro(cpuinfo_transition_latency);
655cpufreq_freq_attr_ro(scaling_available_governors);
656cpufreq_freq_attr_ro(scaling_driver);
657cpufreq_freq_attr_ro(scaling_cur_freq);
658cpufreq_freq_attr_ro(bios_limit);
659cpufreq_freq_attr_ro(related_cpus);
660cpufreq_freq_attr_ro(affected_cpus);
661cpufreq_freq_attr_rw(scaling_min_freq);
662cpufreq_freq_attr_rw(scaling_max_freq);
663cpufreq_freq_attr_rw(scaling_governor);
664cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Dave Jones905d77c2008-03-05 14:28:32 -0500666static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 &cpuinfo_min_freq.attr,
668 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100669 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 &scaling_min_freq.attr,
671 &scaling_max_freq.attr,
672 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700673 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 &scaling_governor.attr,
675 &scaling_driver.attr,
676 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700677 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 NULL
679};
680
Dave Jones29464f22009-01-18 01:37:11 -0500681#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
682#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Dave Jones29464f22009-01-18 01:37:11 -0500684static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Dave Jones905d77c2008-03-05 14:28:32 -0500686 struct cpufreq_policy *policy = to_policy(kobj);
687 struct freq_attr *fattr = to_attr(attr);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500688 ssize_t ret = -EINVAL;
Stephen Boyda9144432012-07-20 18:14:38 +0000689 policy = cpufreq_cpu_get_sysfs(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (!policy)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500691 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800692
693 if (lock_policy_rwsem_read(policy->cpu) < 0)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500694 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800695
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530696 if (fattr->show)
697 ret = fattr->show(policy, buf);
698 else
699 ret = -EIO;
700
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800701 unlock_policy_rwsem_read(policy->cpu);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500702fail:
Stephen Boyda9144432012-07-20 18:14:38 +0000703 cpufreq_cpu_put_sysfs(policy);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500704no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return ret;
706}
707
Dave Jones905d77c2008-03-05 14:28:32 -0500708static ssize_t store(struct kobject *kobj, struct attribute *attr,
709 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Dave Jones905d77c2008-03-05 14:28:32 -0500711 struct cpufreq_policy *policy = to_policy(kobj);
712 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500713 ssize_t ret = -EINVAL;
Stephen Boyda9144432012-07-20 18:14:38 +0000714 policy = cpufreq_cpu_get_sysfs(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (!policy)
Dave Jonesa07530b2008-03-05 14:22:25 -0500716 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800717
718 if (lock_policy_rwsem_write(policy->cpu) < 0)
Dave Jonesa07530b2008-03-05 14:22:25 -0500719 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800720
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530721 if (fattr->store)
722 ret = fattr->store(policy, buf, count);
723 else
724 ret = -EIO;
725
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800726 unlock_policy_rwsem_write(policy->cpu);
Dave Jonesa07530b2008-03-05 14:22:25 -0500727fail:
Stephen Boyda9144432012-07-20 18:14:38 +0000728 cpufreq_cpu_put_sysfs(policy);
Dave Jonesa07530b2008-03-05 14:22:25 -0500729no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return ret;
731}
732
Dave Jones905d77c2008-03-05 14:28:32 -0500733static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Dave Jones905d77c2008-03-05 14:28:32 -0500735 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200736 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 complete(&policy->kobj_unregister);
738}
739
Emese Revfy52cf25d2010-01-19 02:58:23 +0100740static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 .show = show,
742 .store = store,
743};
744
745static struct kobj_type ktype_cpufreq = {
746 .sysfs_ops = &sysfs_ops,
747 .default_attrs = default_attrs,
748 .release = cpufreq_sysfs_release,
749};
750
Viresh Kumar2361be22013-05-17 16:09:09 +0530751struct kobject *cpufreq_global_kobject;
752EXPORT_SYMBOL(cpufreq_global_kobject);
753
754static int cpufreq_global_kobject_usage;
755
756int cpufreq_get_global_kobject(void)
757{
758 if (!cpufreq_global_kobject_usage++)
759 return kobject_add(cpufreq_global_kobject,
760 &cpu_subsys.dev_root->kobj, "%s", "cpufreq");
761
762 return 0;
763}
764EXPORT_SYMBOL(cpufreq_get_global_kobject);
765
766void cpufreq_put_global_kobject(void)
767{
768 if (!--cpufreq_global_kobject_usage)
769 kobject_del(cpufreq_global_kobject);
770}
771EXPORT_SYMBOL(cpufreq_put_global_kobject);
772
773int cpufreq_sysfs_create_file(const struct attribute *attr)
774{
775 int ret = cpufreq_get_global_kobject();
776
777 if (!ret) {
778 ret = sysfs_create_file(cpufreq_global_kobject, attr);
779 if (ret)
780 cpufreq_put_global_kobject();
781 }
782
783 return ret;
784}
785EXPORT_SYMBOL(cpufreq_sysfs_create_file);
786
787void cpufreq_sysfs_remove_file(const struct attribute *attr)
788{
789 sysfs_remove_file(cpufreq_global_kobject, attr);
790 cpufreq_put_global_kobject();
791}
792EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
793
Dave Jones19d6f7e2009-07-08 17:35:39 -0400794/* symlink affected CPUs */
Alex Chiangcf3289d02009-11-17 20:27:08 -0700795static int cpufreq_add_dev_symlink(unsigned int cpu,
796 struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400797{
798 unsigned int j;
799 int ret = 0;
800
801 for_each_cpu(j, policy->cpus) {
802 struct cpufreq_policy *managed_policy;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800803 struct device *cpu_dev;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400804
805 if (j == cpu)
806 continue;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400807
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200808 pr_debug("CPU %u already managed, adding link\n", j);
Dave Jones19d6f7e2009-07-08 17:35:39 -0400809 managed_policy = cpufreq_cpu_get(cpu);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800810 cpu_dev = get_cpu_device(j);
811 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Dave Jones19d6f7e2009-07-08 17:35:39 -0400812 "cpufreq");
813 if (ret) {
814 cpufreq_cpu_put(managed_policy);
815 return ret;
816 }
817 }
818 return ret;
819}
820
Alex Chiangcf3289d02009-11-17 20:27:08 -0700821static int cpufreq_add_dev_interface(unsigned int cpu,
822 struct cpufreq_policy *policy,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800823 struct device *dev)
Dave Jones909a6942009-07-08 18:05:42 -0400824{
Dave Jonesecf7e462009-07-08 18:48:47 -0400825 struct cpufreq_policy new_policy;
Dave Jones909a6942009-07-08 18:05:42 -0400826 struct freq_attr **drv_attr;
827 unsigned long flags;
828 int ret = 0;
829 unsigned int j;
830
831 /* prepare interface data */
832 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800833 &dev->kobj, "cpufreq");
Dave Jones909a6942009-07-08 18:05:42 -0400834 if (ret)
835 return ret;
836
837 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200838 drv_attr = cpufreq_driver->attr;
Dave Jones909a6942009-07-08 18:05:42 -0400839 while ((drv_attr) && (*drv_attr)) {
840 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
841 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200842 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400843 drv_attr++;
844 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200845 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -0400846 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
847 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200848 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400849 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200850 if (cpufreq_driver->target) {
Dave Jones909a6942009-07-08 18:05:42 -0400851 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
852 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200853 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400854 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200855 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +0100856 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
857 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200858 goto err_out_kobj_put;
Thomas Renningere2f74f32009-11-19 12:31:01 +0100859 }
Dave Jones909a6942009-07-08 18:05:42 -0400860
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000861 write_lock_irqsave(&cpufreq_driver_lock, flags);
Dave Jones909a6942009-07-08 18:05:42 -0400862 for_each_cpu(j, policy->cpus) {
Dave Jones909a6942009-07-08 18:05:42 -0400863 per_cpu(cpufreq_cpu_data, j) = policy;
Tejun Heof1625062009-10-29 22:34:13 +0900864 per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
Dave Jones909a6942009-07-08 18:05:42 -0400865 }
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000866 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones909a6942009-07-08 18:05:42 -0400867
868 ret = cpufreq_add_dev_symlink(cpu, policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400869 if (ret)
870 goto err_out_kobj_put;
871
872 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
873 /* assure that the starting sequence is run in __cpufreq_set_policy */
874 policy->governor = NULL;
875
876 /* set default policy */
877 ret = __cpufreq_set_policy(policy, &new_policy);
878 policy->user_policy.policy = policy->policy;
879 policy->user_policy.governor = policy->governor;
880
881 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200882 pr_debug("setting policy failed\n");
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200883 if (cpufreq_driver->exit)
884 cpufreq_driver->exit(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400885 }
Dave Jones909a6942009-07-08 18:05:42 -0400886 return ret;
887
888err_out_kobj_put:
889 kobject_put(&policy->kobj);
890 wait_for_completion(&policy->kobj_unregister);
891 return ret;
892}
893
Viresh Kumarfcf80582013-01-29 14:39:08 +0000894#ifdef CONFIG_HOTPLUG_CPU
895static int cpufreq_add_policy_cpu(unsigned int cpu, unsigned int sibling,
896 struct device *dev)
897{
898 struct cpufreq_policy *policy;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200899 int ret = 0, has_target = !!cpufreq_driver->target;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000900 unsigned long flags;
901
902 policy = cpufreq_cpu_get(sibling);
903 WARN_ON(!policy);
904
Viresh Kumar820c6ca2013-04-22 00:48:03 +0200905 if (has_target)
906 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000907
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530908 lock_policy_rwsem_write(sibling);
909
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000910 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530911
Viresh Kumarfcf80582013-01-29 14:39:08 +0000912 cpumask_set_cpu(cpu, policy->cpus);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530913 per_cpu(cpufreq_policy_cpu, cpu) = policy->cpu;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000914 per_cpu(cpufreq_cpu_data, cpu) = policy;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000915 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000916
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530917 unlock_policy_rwsem_write(sibling);
918
Viresh Kumar820c6ca2013-04-22 00:48:03 +0200919 if (has_target) {
920 __cpufreq_governor(policy, CPUFREQ_GOV_START);
921 __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
922 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000923
Viresh Kumarfcf80582013-01-29 14:39:08 +0000924 ret = sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
925 if (ret) {
926 cpufreq_cpu_put(policy);
927 return ret;
928 }
929
930 return 0;
931}
932#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934/**
935 * cpufreq_add_dev - add a CPU device
936 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500937 * Adds the cpufreq interface for a CPU device.
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400938 *
939 * The Oracle says: try running cpufreq registration/unregistration concurrently
940 * with with cpu hotplugging and all hell will break loose. Tried to clean this
941 * mess up, but more thorough testing is needed. - Mathieu
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800943static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
Viresh Kumarfcf80582013-01-29 14:39:08 +0000945 unsigned int j, cpu = dev->id;
Viresh Kumar65922462013-02-07 10:56:03 +0530946 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 unsigned long flags;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500949#ifdef CONFIG_HOTPLUG_CPU
Viresh Kumarfcf80582013-01-29 14:39:08 +0000950 struct cpufreq_governor *gov;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500951 int sibling;
952#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Ashok Rajc32b6b82005-10-30 14:59:54 -0800954 if (cpu_is_offline(cpu))
955 return 0;
956
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200957 pr_debug("adding CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959#ifdef CONFIG_SMP
960 /* check whether a different CPU already registered this
961 * CPU because it is in the same boat. */
962 policy = cpufreq_cpu_get(cpu);
963 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -0500964 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return 0;
966 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000967
968#ifdef CONFIG_HOTPLUG_CPU
969 /* Check if this cpu was hot-unplugged earlier and has siblings */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000970 read_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000971 for_each_online_cpu(sibling) {
972 struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530973 if (cp && cpumask_test_cpu(cpu, cp->related_cpus)) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000974 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000975 return cpufreq_add_policy_cpu(cpu, sibling, dev);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530976 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000977 }
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000978 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000979#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980#endif
981
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200982 if (!try_module_get(cpufreq_driver->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 ret = -EINVAL;
984 goto module_out;
985 }
986
Dave Jonese98df502005-10-20 15:17:43 -0700987 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
Dave Jones059019a2009-07-08 16:30:03 -0400988 if (!policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 goto nomem_out;
Dave Jones059019a2009-07-08 16:30:03 -0400990
991 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400992 goto err_free_policy;
Dave Jones059019a2009-07-08 16:30:03 -0400993
994 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400995 goto err_free_cpumask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 policy->cpu = cpu;
Viresh Kumar65922462013-02-07 10:56:03 +0530998 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Rusty Russell835481d2009-01-04 05:18:06 -0800999 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001001 /* Initially set CPU itself as the policy_cpu */
Tejun Heof1625062009-10-29 22:34:13 +09001002 per_cpu(cpufreq_policy_cpu, cpu) = cpu;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 init_completion(&policy->kobj_unregister);
David Howells65f27f32006-11-22 14:55:48 +00001005 INIT_WORK(&policy->update, handle_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 /* call driver. From then on the cpufreq must be able
1008 * to accept all calls to ->verify and ->setpolicy for this CPU
1009 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001010 ret = cpufreq_driver->init(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001012 pr_debug("initialization failed\n");
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301013 goto err_set_policy_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001015
Viresh Kumarfcf80582013-01-29 14:39:08 +00001016 /* related cpus should atleast have policy->cpus */
1017 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
1018
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001019 /*
1020 * affected cpus must always be the one, which are online. We aren't
1021 * managing offline cpus here.
1022 */
1023 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1024
Mike Chan187d9f42008-12-04 12:19:17 -08001025 policy->user_policy.min = policy->min;
1026 policy->user_policy.max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Thomas Renningera1531ac2008-07-29 22:32:58 -07001028 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1029 CPUFREQ_START, policy);
1030
Viresh Kumarfcf80582013-01-29 14:39:08 +00001031#ifdef CONFIG_HOTPLUG_CPU
1032 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
1033 if (gov) {
1034 policy->governor = gov;
1035 pr_debug("Restoring governor %s for cpu %d\n",
1036 policy->governor->name, cpu);
Thomas Renninger4bfa0422009-07-24 15:25:03 +02001037 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001038#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001040 ret = cpufreq_add_dev_interface(cpu, policy, dev);
Dave Jones19d6f7e2009-07-08 17:35:39 -04001041 if (ret)
1042 goto err_out_unregister;
Dave Jones8ff69732006-03-05 03:37:23 -05001043
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001044 kobject_uevent(&policy->kobj, KOBJ_ADD);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001045 module_put(cpufreq_driver->owner);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001046 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 return 0;
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050err_out_unregister:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001051 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rusty Russell835481d2009-01-04 05:18:06 -08001052 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -07001053 per_cpu(cpufreq_cpu_data, j) = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001054 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -08001056 kobject_put(&policy->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 wait_for_completion(&policy->kobj_unregister);
1058
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301059err_set_policy_cpu:
1060 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Xiaotian Fengcad70a62010-07-20 20:11:02 +08001061 free_cpumask_var(policy->related_cpus);
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001062err_free_cpumask:
1063 free_cpumask_var(policy->cpus);
1064err_free_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 kfree(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066nomem_out:
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001067 module_put(cpufreq_driver->owner);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001068module_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return ret;
1070}
1071
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001072static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
1073{
1074 int j;
1075
1076 policy->last_cpu = policy->cpu;
1077 policy->cpu = cpu;
1078
Viresh Kumar3361b7b2013-02-04 11:38:51 +00001079 for_each_cpu(j, policy->cpus)
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001080 per_cpu(cpufreq_policy_cpu, j) = cpu;
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001081
1082#ifdef CONFIG_CPU_FREQ_TABLE
1083 cpufreq_frequency_table_update_policy_cpu(policy);
1084#endif
1085 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1086 CPUFREQ_UPDATE_POLICY_CPU, policy);
1087}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089/**
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001090 * __cpufreq_remove_dev - remove a CPU device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 *
1092 * Removes the cpufreq interface for a CPU device.
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001093 * Caller should already have policy_rwsem in write mode for this CPU.
1094 * This routine frees the rwsem before returning.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001096static int __cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001098 unsigned int cpu = dev->id, ret, cpus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 unsigned long flags;
1100 struct cpufreq_policy *data;
Amerigo Wang499bca92010-03-04 03:23:46 -05001101 struct kobject *kobj;
1102 struct completion *cmp;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001103 struct device *cpu_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001105 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001107 write_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 data = per_cpu(cpufreq_cpu_data, cpu);
Mike Travis7a6aedf2008-03-25 15:06:53 -07001110 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001112 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (!data) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001115 pr_debug("%s: No cpu_data found\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001119 if (cpufreq_driver->target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001121
Jacob Shin27ecddc2011-04-27 13:32:11 -05001122#ifdef CONFIG_HOTPLUG_CPU
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001123 if (!cpufreq_driver->setpolicy)
Dirk Brandewiefa69e332013-02-06 09:02:11 -08001124 strncpy(per_cpu(cpufreq_cpu_governor, cpu),
1125 data->governor->name, CPUFREQ_NAME_LEN);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001126#endif
1127
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301128 WARN_ON(lock_policy_rwsem_write(cpu));
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001129 cpus = cpumask_weight(data->cpus);
Viresh Kumare4969eb2013-04-11 08:04:53 +00001130
1131 if (cpus > 1)
1132 cpumask_clear_cpu(cpu, data->cpus);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301133 unlock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Viresh Kumar73bf0fc2013-02-05 22:21:14 +01001135 if (cpu != data->cpu) {
1136 sysfs_remove_link(&dev->kobj, "cpufreq");
1137 } else if (cpus > 1) {
Venki Pallipadiec282972007-03-26 12:03:19 -07001138 /* first sibling now owns the new sysfs dir */
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001139 cpu_dev = get_cpu_device(cpumask_first(data->cpus));
1140 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
1141 ret = kobject_move(&data->kobj, &cpu_dev->kobj);
1142 if (ret) {
1143 pr_err("%s: Failed to move kobj: %d", __func__, ret);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301144
1145 WARN_ON(lock_policy_rwsem_write(cpu));
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001146 cpumask_set_cpu(cpu, data->cpus);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301147
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001148 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301149 per_cpu(cpufreq_cpu_data, cpu) = data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001150 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301151
1152 unlock_policy_rwsem_write(cpu);
1153
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001154 ret = sysfs_create_link(&cpu_dev->kobj, &data->kobj,
1155 "cpufreq");
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001156 return -EINVAL;
1157 }
Venki Pallipadiec282972007-03-26 12:03:19 -07001158
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301159 WARN_ON(lock_policy_rwsem_write(cpu));
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001160 update_policy_cpu(data, cpu_dev->id);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301161 unlock_policy_rwsem_write(cpu);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001162 pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
1163 __func__, cpu_dev->id, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001164 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001165
Viresh Kumard96038e2013-04-30 14:32:18 +00001166 if ((cpus == 1) && (cpufreq_driver->target))
1167 __cpufreq_governor(data, CPUFREQ_GOV_POLICY_EXIT);
1168
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001169 pr_debug("%s: removing link, cpu: %d\n", __func__, cpu);
1170 cpufreq_cpu_put(data);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001171
1172 /* If cpu is last user of policy, free policy */
1173 if (cpus == 1) {
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301174 lock_policy_rwsem_read(cpu);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001175 kobj = &data->kobj;
1176 cmp = &data->kobj_unregister;
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301177 unlock_policy_rwsem_read(cpu);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001178 kobject_put(kobj);
1179
1180 /* we need to make sure that the underlying kobj is actually
1181 * not referenced anymore by anybody before we proceed with
1182 * unloading.
1183 */
1184 pr_debug("waiting for dropping of refcount\n");
1185 wait_for_completion(cmp);
1186 pr_debug("wait complete\n");
1187
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001188 if (cpufreq_driver->exit)
1189 cpufreq_driver->exit(data);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001190
1191 free_cpumask_var(data->related_cpus);
1192 free_cpumask_var(data->cpus);
1193 kfree(data);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001194 } else if (cpufreq_driver->target) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001195 __cpufreq_governor(data, CPUFREQ_GOV_START);
1196 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301199 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 return 0;
1201}
1202
1203
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001204static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001205{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001206 unsigned int cpu = dev->id;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001207 int retval;
1208
1209 if (cpu_is_offline(cpu))
1210 return 0;
1211
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001212 retval = __cpufreq_remove_dev(dev, sif);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001213 return retval;
1214}
1215
1216
David Howells65f27f32006-11-22 14:55:48 +00001217static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
David Howells65f27f32006-11-22 14:55:48 +00001219 struct cpufreq_policy *policy =
1220 container_of(work, struct cpufreq_policy, update);
1221 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001222 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 cpufreq_update_policy(cpu);
1224}
1225
1226/**
1227 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
1228 * @cpu: cpu number
1229 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1230 * @new_freq: CPU frequency the CPU actually runs at
1231 *
Dave Jones29464f22009-01-18 01:37:11 -05001232 * We adjust to current frequency first, and need to clean up later.
1233 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301235static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1236 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301238 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301240 unsigned long flags;
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001243 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 freqs.old = old_freq;
1247 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301248
1249 read_lock_irqsave(&cpufreq_driver_lock, flags);
1250 policy = per_cpu(cpufreq_cpu_data, cpu);
1251 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1252
1253 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1254 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255}
1256
1257
Dave Jones32ee8c32006-02-28 00:43:23 -05001258/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301259 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001260 * @cpu: CPU number
1261 *
1262 * This is the last known freq, without actually getting it from the driver.
1263 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1264 */
1265unsigned int cpufreq_quick_get(unsigned int cpu)
1266{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001267 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301268 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001269
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001270 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1271 return cpufreq_driver->get(cpu);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001272
1273 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001274 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301275 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001276 cpufreq_cpu_put(policy);
1277 }
1278
Dave Jones4d34a672008-02-07 16:33:49 -05001279 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001280}
1281EXPORT_SYMBOL(cpufreq_quick_get);
1282
Jesse Barnes3d737102011-06-28 10:59:12 -07001283/**
1284 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1285 * @cpu: CPU number
1286 *
1287 * Just return the max possible frequency for a given CPU.
1288 */
1289unsigned int cpufreq_quick_get_max(unsigned int cpu)
1290{
1291 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1292 unsigned int ret_freq = 0;
1293
1294 if (policy) {
1295 ret_freq = policy->max;
1296 cpufreq_cpu_put(policy);
1297 }
1298
1299 return ret_freq;
1300}
1301EXPORT_SYMBOL(cpufreq_quick_get_max);
1302
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001303
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001304static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001306 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301307 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001309 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001310 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001312 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301314 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001315 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301316 /* verify no discrepancy between actual and
1317 saved value exists */
1318 if (unlikely(ret_freq != policy->cur)) {
1319 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 schedule_work(&policy->update);
1321 }
1322 }
1323
Dave Jones4d34a672008-02-07 16:33:49 -05001324 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001325}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001327/**
1328 * cpufreq_get - get the current CPU frequency (in kHz)
1329 * @cpu: CPU number
1330 *
1331 * Get the CPU current (static) CPU frequency
1332 */
1333unsigned int cpufreq_get(unsigned int cpu)
1334{
1335 unsigned int ret_freq = 0;
1336 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1337
1338 if (!policy)
1339 goto out;
1340
1341 if (unlikely(lock_policy_rwsem_read(cpu)))
1342 goto out_policy;
1343
1344 ret_freq = __cpufreq_get(cpu);
1345
1346 unlock_policy_rwsem_read(cpu);
1347
1348out_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 cpufreq_cpu_put(policy);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001350out:
Dave Jones4d34a672008-02-07 16:33:49 -05001351 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352}
1353EXPORT_SYMBOL(cpufreq_get);
1354
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001355static struct subsys_interface cpufreq_interface = {
1356 .name = "cpufreq",
1357 .subsys = &cpu_subsys,
1358 .add_dev = cpufreq_add_dev,
1359 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001360};
1361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001364 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1365 *
1366 * This function is only executed for the boot processor. The other CPUs
1367 * have been put offline by means of CPU hotplug.
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001368 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001369static int cpufreq_bp_suspend(void)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001370{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301371 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001372
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001373 int cpu = smp_processor_id();
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001374 struct cpufreq_policy *cpu_policy;
1375
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001376 pr_debug("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001377
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001378 /* If there's no policy for the boot CPU, we have nothing to do. */
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001379 cpu_policy = cpufreq_cpu_get(cpu);
1380 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001381 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001382
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001383 if (cpufreq_driver->suspend) {
1384 ret = cpufreq_driver->suspend(cpu_policy);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001385 if (ret)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001386 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1387 "step on CPU %u\n", cpu_policy->cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001388 }
1389
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001390 cpufreq_cpu_put(cpu_policy);
Dave Jonesc9060492008-02-07 16:32:18 -05001391 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001392}
1393
1394/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001395 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 *
1397 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001398 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1399 * restored. It will verify that the current freq is in sync with
1400 * what we believe it to be. This is a bit later than when it
1401 * should be, but nonethteless it's better than calling
1402 * cpufreq_driver->get() here which might re-enable interrupts...
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001403 *
1404 * This function is only executed for the boot CPU. The other CPUs have not
1405 * been turned on yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001407static void cpufreq_bp_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301409 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001410
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001411 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 struct cpufreq_policy *cpu_policy;
1413
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001414 pr_debug("resuming cpu %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001416 /* If there's no policy for the boot CPU, we have nothing to do. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 cpu_policy = cpufreq_cpu_get(cpu);
1418 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001419 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001421 if (cpufreq_driver->resume) {
1422 ret = cpufreq_driver->resume(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 if (ret) {
1424 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1425 "step on CPU %u\n", cpu_policy->cpu);
Dave Jonesc9060492008-02-07 16:32:18 -05001426 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
1428 }
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 schedule_work(&cpu_policy->update);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001431
Dave Jonesc9060492008-02-07 16:32:18 -05001432fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434}
1435
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001436static struct syscore_ops cpufreq_syscore_ops = {
1437 .suspend = cpufreq_bp_suspend,
1438 .resume = cpufreq_bp_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439};
1440
Borislav Petkov9d950462013-01-20 10:24:28 +00001441/**
1442 * cpufreq_get_current_driver - return current driver's name
1443 *
1444 * Return the name string of the currently loaded cpufreq driver
1445 * or NULL, if none.
1446 */
1447const char *cpufreq_get_current_driver(void)
1448{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001449 if (cpufreq_driver)
1450 return cpufreq_driver->name;
1451
1452 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001453}
1454EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456/*********************************************************************
1457 * NOTIFIER LISTS INTERFACE *
1458 *********************************************************************/
1459
1460/**
1461 * cpufreq_register_notifier - register a driver with cpufreq
1462 * @nb: notifier function to register
1463 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1464 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001465 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 * are notified about clock rate changes (once before and once after
1467 * the transition), or a list of drivers that are notified about
1468 * changes in cpufreq policy.
1469 *
1470 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001471 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 */
1473int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1474{
1475 int ret;
1476
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001477 if (cpufreq_disabled())
1478 return -EINVAL;
1479
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001480 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 switch (list) {
1483 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001484 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001485 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 break;
1487 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001488 ret = blocking_notifier_chain_register(
1489 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 break;
1491 default:
1492 ret = -EINVAL;
1493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495 return ret;
1496}
1497EXPORT_SYMBOL(cpufreq_register_notifier);
1498
1499
1500/**
1501 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1502 * @nb: notifier block to be unregistered
1503 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1504 *
1505 * Remove a driver from the CPU frequency notifier list.
1506 *
1507 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001508 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 */
1510int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1511{
1512 int ret;
1513
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001514 if (cpufreq_disabled())
1515 return -EINVAL;
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 switch (list) {
1518 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001519 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001520 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 break;
1522 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001523 ret = blocking_notifier_chain_unregister(
1524 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 break;
1526 default:
1527 ret = -EINVAL;
1528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530 return ret;
1531}
1532EXPORT_SYMBOL(cpufreq_unregister_notifier);
1533
1534
1535/*********************************************************************
1536 * GOVERNORS *
1537 *********************************************************************/
1538
1539
1540int __cpufreq_driver_target(struct cpufreq_policy *policy,
1541 unsigned int target_freq,
1542 unsigned int relation)
1543{
1544 int retval = -EINVAL;
Viresh Kumar72499242012-10-31 01:28:21 +01001545 unsigned int old_target_freq = target_freq;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001546
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001547 if (cpufreq_disabled())
1548 return -ENODEV;
1549
Viresh Kumar72499242012-10-31 01:28:21 +01001550 /* Make sure that target_freq is within supported range */
1551 if (target_freq > policy->max)
1552 target_freq = policy->max;
1553 if (target_freq < policy->min)
1554 target_freq = policy->min;
1555
1556 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1557 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001558
1559 if (target_freq == policy->cur)
1560 return 0;
1561
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001562 if (cpufreq_driver->target)
1563 retval = cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 return retval;
1566}
1567EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569int cpufreq_driver_target(struct cpufreq_policy *policy,
1570 unsigned int target_freq,
1571 unsigned int relation)
1572{
Julia Lawallf1829e42008-07-25 22:44:53 +02001573 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
1575 policy = cpufreq_cpu_get(policy->cpu);
1576 if (!policy)
Julia Lawallf1829e42008-07-25 22:44:53 +02001577 goto no_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001579 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
Julia Lawallf1829e42008-07-25 22:44:53 +02001580 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582 ret = __cpufreq_driver_target(policy, target_freq, relation);
1583
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001584 unlock_policy_rwsem_write(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Julia Lawallf1829e42008-07-25 22:44:53 +02001586fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 cpufreq_cpu_put(policy);
Julia Lawallf1829e42008-07-25 22:44:53 +02001588no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 return ret;
1590}
1591EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1592
venkatesh.pallipadi@intel.combf0b90e2008-08-04 11:59:07 -07001593int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu)
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001594{
1595 int ret = 0;
1596
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001597 if (cpufreq_disabled())
1598 return ret;
1599
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001600 if (!cpufreq_driver->getavg)
Viresh Kumar0676f7f2012-10-24 23:39:48 +02001601 return 0;
1602
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001603 policy = cpufreq_cpu_get(policy->cpu);
1604 if (!policy)
1605 return -EINVAL;
1606
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001607 ret = cpufreq_driver->getavg(policy, cpu);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001608
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001609 cpufreq_cpu_put(policy);
1610 return ret;
1611}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001612EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001613
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001614/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001615 * when "event" is CPUFREQ_GOV_LIMITS
1616 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301618static int __cpufreq_governor(struct cpufreq_policy *policy,
1619 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
Dave Jonescc993ca2005-07-28 09:43:56 -07001621 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001622
1623 /* Only must be defined when default governor is known to have latency
1624 restrictions, like e.g. conservative or ondemand.
1625 That this is the case is already ensured in Kconfig
1626 */
1627#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1628 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1629#else
1630 struct cpufreq_governor *gov = NULL;
1631#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001632
1633 if (policy->governor->max_transition_latency &&
1634 policy->cpuinfo.transition_latency >
1635 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07001636 if (!gov)
1637 return -EINVAL;
1638 else {
1639 printk(KERN_WARNING "%s governor failed, too long"
1640 " transition latency of HW, fallback"
1641 " to %s governor\n",
1642 policy->governor->name,
1643 gov->name);
1644 policy->governor = gov;
1645 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
1648 if (!try_module_get(policy->governor->owner))
1649 return -EINVAL;
1650
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001651 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301652 policy->cpu, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 ret = policy->governor->governor(policy, event);
1654
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001655 if (!ret) {
1656 if (event == CPUFREQ_GOV_POLICY_INIT)
1657 policy->governor->initialized++;
1658 else if (event == CPUFREQ_GOV_POLICY_EXIT)
1659 policy->governor->initialized--;
1660 }
Viresh Kumarb3940582013-02-01 05:42:58 +00001661
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301662 /* we keep one module reference alive for
1663 each CPU governed by this CPU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 if ((event != CPUFREQ_GOV_START) || ret)
1665 module_put(policy->governor->owner);
1666 if ((event == CPUFREQ_GOV_STOP) && !ret)
1667 module_put(policy->governor->owner);
1668
1669 return ret;
1670}
1671
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673int cpufreq_register_governor(struct cpufreq_governor *governor)
1674{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001675 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
1677 if (!governor)
1678 return -EINVAL;
1679
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001680 if (cpufreq_disabled())
1681 return -ENODEV;
1682
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001683 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001684
Viresh Kumarb3940582013-02-01 05:42:58 +00001685 governor->initialized = 0;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001686 err = -EBUSY;
1687 if (__find_governor(governor->name) == NULL) {
1688 err = 0;
1689 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Dave Jones32ee8c32006-02-28 00:43:23 -05001692 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001693 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694}
1695EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1696
1697
1698void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1699{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001700#ifdef CONFIG_HOTPLUG_CPU
1701 int cpu;
1702#endif
1703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 if (!governor)
1705 return;
1706
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001707 if (cpufreq_disabled())
1708 return;
1709
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001710#ifdef CONFIG_HOTPLUG_CPU
1711 for_each_present_cpu(cpu) {
1712 if (cpu_online(cpu))
1713 continue;
1714 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1715 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1716 }
1717#endif
1718
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001719 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001721 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 return;
1723}
1724EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1725
1726
1727
1728/*********************************************************************
1729 * POLICY INTERFACE *
1730 *********************************************************************/
1731
1732/**
1733 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05001734 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1735 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 *
1737 * Reads the current cpufreq policy.
1738 */
1739int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1740{
1741 struct cpufreq_policy *cpu_policy;
1742 if (!policy)
1743 return -EINVAL;
1744
1745 cpu_policy = cpufreq_cpu_get(cpu);
1746 if (!cpu_policy)
1747 return -EINVAL;
1748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 return 0;
1753}
1754EXPORT_SYMBOL(cpufreq_get_policy);
1755
1756
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001757/*
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301758 * data : current policy.
1759 * policy : policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001760 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301761static int __cpufreq_set_policy(struct cpufreq_policy *data,
1762 struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763{
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001764 int ret = 0, failed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001766 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 policy->min, policy->max);
1768
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301769 memcpy(&policy->cpuinfo, &data->cpuinfo,
1770 sizeof(struct cpufreq_cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Yi Yang53391fa2008-01-30 13:33:34 +01001772 if (policy->min > data->max || policy->max < data->min) {
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02001773 ret = -EINVAL;
1774 goto error_out;
1775 }
1776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 /* verify the cpu speed can be set within this limit */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001778 ret = cpufreq_driver->verify(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 if (ret)
1780 goto error_out;
1781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001783 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1784 CPUFREQ_ADJUST, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
1786 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001787 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1788 CPUFREQ_INCOMPATIBLE, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
1790 /* verify the cpu speed can be set within this limit,
1791 which might be different to the first one */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001792 ret = cpufreq_driver->verify(policy);
Alan Sterne041c682006-03-27 01:16:30 -08001793 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001797 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1798 CPUFREQ_NOTIFY, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Dave Jones7d5e3502006-02-02 17:03:42 -05001800 data->min = policy->min;
1801 data->max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001803 pr_debug("new min and max freqs are %u - %u kHz\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301804 data->min, data->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001806 if (cpufreq_driver->setpolicy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 data->policy = policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001808 pr_debug("setting range\n");
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001809 ret = cpufreq_driver->setpolicy(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 } else {
1811 if (policy->governor != data->governor) {
1812 /* save old, working values */
1813 struct cpufreq_governor *old_gov = data->governor;
1814
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001815 pr_debug("governor switch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
1817 /* end old governor */
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001818 if (data->governor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Viresh Kumar955ef482013-05-16 05:09:58 +00001820 unlock_policy_rwsem_write(policy->cpu);
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001821 __cpufreq_governor(data,
1822 CPUFREQ_GOV_POLICY_EXIT);
Viresh Kumar955ef482013-05-16 05:09:58 +00001823 lock_policy_rwsem_write(policy->cpu);
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
1826 /* start new governor */
1827 data->governor = policy->governor;
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001828 if (!__cpufreq_governor(data, CPUFREQ_GOV_POLICY_INIT)) {
Viresh Kumar955ef482013-05-16 05:09:58 +00001829 if (!__cpufreq_governor(data, CPUFREQ_GOV_START)) {
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001830 failed = 0;
Viresh Kumar955ef482013-05-16 05:09:58 +00001831 } else {
1832 unlock_policy_rwsem_write(policy->cpu);
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001833 __cpufreq_governor(data,
1834 CPUFREQ_GOV_POLICY_EXIT);
Viresh Kumar955ef482013-05-16 05:09:58 +00001835 lock_policy_rwsem_write(policy->cpu);
1836 }
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001837 }
1838
1839 if (failed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 /* new governor failed, so re-start old one */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001841 pr_debug("starting governor %s failed\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301842 data->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 if (old_gov) {
1844 data->governor = old_gov;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301845 __cpufreq_governor(data,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001846 CPUFREQ_GOV_POLICY_INIT);
1847 __cpufreq_governor(data,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301848 CPUFREQ_GOV_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 }
1850 ret = -EINVAL;
1851 goto error_out;
1852 }
1853 /* might be a policy change, too, so fall through */
1854 }
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001855 pr_debug("governor: change or update limits\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1857 }
1858
Dave Jones7d5e3502006-02-02 17:03:42 -05001859error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 return ret;
1861}
1862
1863/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1865 * @cpu: CPU which shall be re-evaluated
1866 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001867 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 * at different times.
1869 */
1870int cpufreq_update_policy(unsigned int cpu)
1871{
1872 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1873 struct cpufreq_policy policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02001874 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Julia Lawallf1829e42008-07-25 22:44:53 +02001876 if (!data) {
1877 ret = -ENODEV;
1878 goto no_policy;
1879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Julia Lawallf1829e42008-07-25 22:44:53 +02001881 if (unlikely(lock_policy_rwsem_write(cpu))) {
1882 ret = -EINVAL;
1883 goto fail;
1884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001886 pr_debug("updating policy for CPU %u\n", cpu);
Dave Jones7d5e3502006-02-02 17:03:42 -05001887 memcpy(&policy, data, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 policy.min = data->user_policy.min;
1889 policy.max = data->user_policy.max;
1890 policy.policy = data->user_policy.policy;
1891 policy.governor = data->user_policy.governor;
1892
Thomas Renninger0961dd02006-01-26 18:46:33 +01001893 /* BIOS might change freq behind our back
1894 -> ask driver for current freq and notify governors about a change */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001895 if (cpufreq_driver->get) {
1896 policy.cur = cpufreq_driver->get(cpu);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001897 if (!data->cur) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001898 pr_debug("Driver did not initialize current freq");
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001899 data->cur = policy.cur;
1900 } else {
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001901 if (data->cur != policy.cur && cpufreq_driver->target)
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301902 cpufreq_out_of_sync(cpu, data->cur,
1903 policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001904 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01001905 }
1906
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 ret = __cpufreq_set_policy(data, &policy);
1908
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001909 unlock_policy_rwsem_write(cpu);
1910
Julia Lawallf1829e42008-07-25 22:44:53 +02001911fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 cpufreq_cpu_put(data);
Julia Lawallf1829e42008-07-25 22:44:53 +02001913no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 return ret;
1915}
1916EXPORT_SYMBOL(cpufreq_update_policy);
1917
Satyam Sharmadd184a02007-10-02 13:28:14 -07001918static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08001919 unsigned long action, void *hcpu)
1920{
1921 unsigned int cpu = (unsigned long)hcpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001922 struct device *dev;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001923
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001924 dev = get_cpu_device(cpu);
1925 if (dev) {
Ashok Rajc32b6b82005-10-30 14:59:54 -08001926 switch (action) {
1927 case CPU_ONLINE:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001928 cpufreq_add_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001929 break;
1930 case CPU_DOWN_PREPARE:
Srivatsa S. Bhata66b2e52013-05-15 21:47:17 +02001931 case CPU_UP_CANCELED_FROZEN:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001932 __cpufreq_remove_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001933 break;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001934 case CPU_DOWN_FAILED:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001935 cpufreq_add_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001936 break;
1937 }
1938 }
1939 return NOTIFY_OK;
1940}
1941
Neal Buckendahl9c36f742010-06-22 22:02:44 -05001942static struct notifier_block __refdata cpufreq_cpu_notifier = {
Ashok Rajc32b6b82005-10-30 14:59:54 -08001943 .notifier_call = cpufreq_cpu_callback,
1944};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946/*********************************************************************
1947 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1948 *********************************************************************/
1949
1950/**
1951 * cpufreq_register_driver - register a CPU Frequency driver
1952 * @driver_data: A struct cpufreq_driver containing the values#
1953 * submitted by the CPU Frequency driver.
1954 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001955 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05001957 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 *
1959 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001960int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961{
1962 unsigned long flags;
1963 int ret;
1964
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001965 if (cpufreq_disabled())
1966 return -ENODEV;
1967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 if (!driver_data || !driver_data->verify || !driver_data->init ||
1969 ((!driver_data->setpolicy) && (!driver_data->target)))
1970 return -EINVAL;
1971
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001972 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
1974 if (driver_data->setpolicy)
1975 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1976
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001977 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001978 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001979 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 return -EBUSY;
1981 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001982 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001983 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001985 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001986 if (ret)
1987 goto err_null_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001989 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 int i;
1991 ret = -ENODEV;
1992
1993 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07001994 for (i = 0; i < nr_cpu_ids; i++)
1995 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07001997 break;
1998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
2000 /* if all ->init() calls failed, unregister */
2001 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002002 pr_debug("no CPU initialized for driver %s\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05302003 driver_data->name);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002004 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 }
2006 }
2007
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002008 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002009 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002011 return 0;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002012err_if_unreg:
2013 subsys_interface_unregister(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002014err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002015 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002016 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002017 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05002018 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019}
2020EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2021
2022
2023/**
2024 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2025 *
Dave Jones32ee8c32006-02-28 00:43:23 -05002026 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 * the right to do so, i.e. if you have succeeded in initialising before!
2028 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2029 * currently not initialised.
2030 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002031int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
2033 unsigned long flags;
2034
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002035 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002038 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002040 subsys_interface_unregister(&cpufreq_interface);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07002041 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002043 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002044 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002045 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
2047 return 0;
2048}
2049EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002050
2051static int __init cpufreq_core_init(void)
2052{
2053 int cpu;
2054
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002055 if (cpufreq_disabled())
2056 return -ENODEV;
2057
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002058 for_each_possible_cpu(cpu) {
Tejun Heof1625062009-10-29 22:34:13 +09002059 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002060 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
2061 }
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002062
Viresh Kumar2361be22013-05-17 16:09:09 +05302063 cpufreq_global_kobject = kobject_create();
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002064 BUG_ON(!cpufreq_global_kobject);
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01002065 register_syscore_ops(&cpufreq_syscore_ops);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002066
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002067 return 0;
2068}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002069core_initcall(cpufreq_core_init);