blob: 3a811df43e146f908dd272a196a9f2cde86395d8 [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>
Viresh Kumarbb176f72013-06-19 14:19:33 +05306 * (C) 2013 Viresh Kumar <viresh.kumar@linaro.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Ashok Rajc32b6b82005-10-30 14:59:54 -08008 * Oct 2005 - Ashok Raj <ashok.raj@intel.com>
Dave Jones32ee8c32006-02-28 00:43:23 -05009 * Added handling for CPU hotplug
Dave Jones8ff69732006-03-05 03:37:23 -050010 * Feb 2006 - Jacob Shin <jacob.shin@amd.com>
11 * Fix handling for CPU hotplug -- affected CPUs
Ashok Rajc32b6b82005-10-30 14:59:54 -080012 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Viresh Kumardb701152012-10-23 01:29:03 +020018#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
Viresh Kumar5ff0a262013-08-06 22:53:03 +053020#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/cpufreq.h>
22#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/device.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053024#include <linux/init.h>
25#include <linux/kernel_stat.h>
26#include <linux/module.h>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080027#include <linux/mutex.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053028#include <linux/slab.h>
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +010029#include <linux/syscore_ops.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053030#include <linux/tick.h>
Thomas Renninger6f4f2722010-04-20 13:17:36 +020031#include <trace/events/power.h>
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/**
Dave Jonescd878472006-08-11 17:59:28 -040034 * The "cpufreq driver" - the arch- or hardware-dependent low
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 * level driver of CPUFreq support, and its spinlock. This lock
36 * also protects the cpufreq_cpu_data array.
37 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +020038static struct cpufreq_driver *cpufreq_driver;
Mike Travis7a6aedf2008-03-25 15:06:53 -070039static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +053040static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data_fallback);
Viresh Kumarbb176f72013-06-19 14:19:33 +053041static DEFINE_RWLOCK(cpufreq_driver_lock);
42static DEFINE_MUTEX(cpufreq_governor_lock);
Lukasz Majewskic88a1f82013-08-06 22:53:08 +053043static LIST_HEAD(cpufreq_policy_list);
Viresh Kumarbb176f72013-06-19 14:19:33 +053044
Thomas Renninger084f3492007-07-09 11:35:28 -070045#ifdef CONFIG_HOTPLUG_CPU
46/* This one keeps track of the previously set governor of a removed CPU */
Dmitry Monakhove77b89f2009-10-05 00:38:55 +040047static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
Thomas Renninger084f3492007-07-09 11:35:28 -070048#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080050/*
51 * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
52 * all cpufreq/hotplug/workqueue/etc related lock issues.
53 *
54 * The rules for this semaphore:
55 * - Any routine that wants to read from the policy structure will
56 * do a down_read on this semaphore.
57 * - Any routine that will write to the policy structure and/or may take away
58 * the policy altogether (eg. CPU hotplug), will hold this lock in write
59 * mode before doing so.
60 *
61 * Additional rules:
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080062 * - Governor routines that can be called in cpufreq hotplug path should not
63 * take this sem as top level hotplug notifier handler takes this.
Mathieu Desnoyers395913d2009-06-08 13:17:31 -040064 * - Lock should not be held across
65 * __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080066 */
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080067static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
68
69#define lock_policy_rwsem(mode, cpu) \
Viresh Kumar1b750e32013-10-02 14:13:09 +053070static void lock_policy_rwsem_##mode(int cpu) \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080071{ \
Viresh Kumar474deff2013-08-20 12:08:25 +053072 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); \
73 BUG_ON(!policy); \
74 down_##mode(&per_cpu(cpu_policy_rwsem, policy->cpu)); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080075}
76
77lock_policy_rwsem(read, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080078lock_policy_rwsem(write, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080079
Viresh Kumarfa1d8af2013-02-07 15:38:42 +053080#define unlock_policy_rwsem(mode, cpu) \
81static void unlock_policy_rwsem_##mode(int cpu) \
82{ \
Viresh Kumar474deff2013-08-20 12:08:25 +053083 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); \
84 BUG_ON(!policy); \
85 up_##mode(&per_cpu(cpu_policy_rwsem, policy->cpu)); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080086}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080087
Viresh Kumarfa1d8af2013-02-07 15:38:42 +053088unlock_policy_rwsem(read, cpu);
89unlock_policy_rwsem(write, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080090
Viresh Kumar6eed9402013-08-06 22:53:11 +053091/*
92 * rwsem to guarantee that cpufreq driver module doesn't unload during critical
93 * sections
94 */
95static DECLARE_RWSEM(cpufreq_rwsem);
96
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
Viresh Kumar6eed9402013-08-06 22:53:11 +0530184struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530186 struct cpufreq_policy *policy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 unsigned long flags;
188
Viresh Kumar6eed9402013-08-06 22:53:11 +0530189 if (cpufreq_disabled() || (cpu >= nr_cpu_ids))
190 return NULL;
191
192 if (!down_read_trylock(&cpufreq_rwsem))
193 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 /* get the cpufreq driver */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000196 read_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Viresh Kumar6eed9402013-08-06 22:53:11 +0530198 if (cpufreq_driver) {
199 /* get the CPU */
200 policy = per_cpu(cpufreq_cpu_data, cpu);
201 if (policy)
202 kobject_get(&policy->kobj);
203 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200204
Viresh Kumar6eed9402013-08-06 22:53:11 +0530205 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530207 if (!policy)
Viresh Kumar6eed9402013-08-06 22:53:11 +0530208 up_read(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530210 return policy;
Stephen Boyda9144432012-07-20 18:14:38 +0000211}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
213
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530214void cpufreq_cpu_put(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000216 if (cpufreq_disabled())
217 return;
218
Viresh Kumar6eed9402013-08-06 22:53:11 +0530219 kobject_put(&policy->kobj);
220 up_read(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
226 *********************************************************************/
227
228/**
229 * adjust_jiffies - adjust the system "loops_per_jiffy"
230 *
231 * This function alters the system "loops_per_jiffy" for the clock
232 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500233 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 * per-CPU loops_per_jiffy value wherever possible.
235 */
236#ifndef CONFIG_SMP
237static unsigned long l_p_j_ref;
Viresh Kumarbb176f72013-06-19 14:19:33 +0530238static unsigned int l_p_j_ref_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Arjan van de Ven858119e2006-01-14 13:20:43 -0800240static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 if (ci->flags & CPUFREQ_CONST_LOOPS)
243 return;
244
245 if (!l_p_j_ref_freq) {
246 l_p_j_ref = loops_per_jiffy;
247 l_p_j_ref_freq = ci->old;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200248 pr_debug("saving %lu as reference value for loops_per_jiffy; "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530249 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
Viresh Kumarbb176f72013-06-19 14:19:33 +0530251 if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700252 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530253 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
254 ci->new);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200255 pr_debug("scaling loops_per_jiffy to %lu "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530256 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258}
259#else
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530260static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
261{
262 return;
263}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264#endif
265
Viresh Kumar0956df9c2013-06-19 14:19:34 +0530266static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530267 struct cpufreq_freqs *freqs, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 BUG_ON(irqs_disabled());
270
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000271 if (cpufreq_disabled())
272 return;
273
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200274 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200275 pr_debug("notification %u of frequency transition to %u kHz\n",
Dave Jonese4472cb2006-01-31 15:53:55 -0800276 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500281 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800282 * which is not equal to what the cpufreq core thinks is
283 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200285 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800286 if ((policy) && (policy->cpu == freqs->cpu) &&
287 (policy->cur) && (policy->cur != freqs->old)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200288 pr_debug("Warning: CPU frequency is"
Dave Jonese4472cb2006-01-31 15:53:55 -0800289 " %u, cpufreq assumed %u kHz.\n",
290 freqs->old, policy->cur);
291 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700294 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800295 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
297 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 case CPUFREQ_POSTCHANGE:
300 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200301 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
Thomas Renninger6f4f2722010-04-20 13:17:36 +0200302 (unsigned long)freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100303 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700304 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800305 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800306 if (likely(policy) && likely(policy->cpu == freqs->cpu))
307 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 break;
309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
Viresh Kumarbb176f72013-06-19 14:19:33 +0530311
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530312/**
313 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
314 * on frequency transition.
315 *
316 * This function calls the transition notifiers and the "adjust_jiffies"
317 * function. It is called twice on all CPU frequency changes that have
318 * external effects.
319 */
320void cpufreq_notify_transition(struct cpufreq_policy *policy,
321 struct cpufreq_freqs *freqs, unsigned int state)
322{
323 for_each_cpu(freqs->cpu, policy->cpus)
324 __cpufreq_notify_transition(policy, freqs, state);
325}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
327
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329/*********************************************************************
330 * SYSFS INTERFACE *
331 *********************************************************************/
332
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700333static struct cpufreq_governor *__find_governor(const char *str_governor)
334{
335 struct cpufreq_governor *t;
336
337 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
Dave Jones29464f22009-01-18 01:37:11 -0500338 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700339 return t;
340
341 return NULL;
342}
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344/**
345 * cpufreq_parse_governor - parse a governor string
346 */
Dave Jones905d77c2008-03-05 14:28:32 -0500347static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 struct cpufreq_governor **governor)
349{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700350 int err = -EINVAL;
351
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200352 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700353 goto out;
354
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200355 if (cpufreq_driver->setpolicy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
357 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700358 err = 0;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530359 } else if (!strnicmp(str_governor, "powersave",
360 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700362 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200364 } else if (cpufreq_driver->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700366
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800367 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700368
369 t = __find_governor(str_governor);
370
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700371 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700372 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700373
Kees Cook1a8e1462011-05-04 08:38:56 -0700374 mutex_unlock(&cpufreq_governor_mutex);
375 ret = request_module("cpufreq_%s", str_governor);
376 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700377
Kees Cook1a8e1462011-05-04 08:38:56 -0700378 if (ret == 0)
379 t = __find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700380 }
381
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700382 if (t != NULL) {
383 *governor = t;
384 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700386
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800387 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
Dave Jones29464f22009-01-18 01:37:11 -0500389out:
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700390 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530394 * cpufreq_per_cpu_attr_read() / show_##file_name() -
395 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 *
397 * Write out information from cpufreq_driver->policy[cpu]; object must be
398 * "unsigned int".
399 */
400
Dave Jones32ee8c32006-02-28 00:43:23 -0500401#define show_one(file_name, object) \
402static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500403(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500404{ \
Dave Jones29464f22009-01-18 01:37:11 -0500405 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
408show_one(cpuinfo_min_freq, cpuinfo.min_freq);
409show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100410show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411show_one(scaling_min_freq, min);
412show_one(scaling_max_freq, max);
413show_one(scaling_cur_freq, cur);
414
Viresh Kumar037ce832013-10-02 14:13:16 +0530415static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530416 struct cpufreq_policy *new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418/**
419 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
420 */
421#define store_one(file_name, object) \
422static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500423(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{ \
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530425 int ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 struct cpufreq_policy new_policy; \
427 \
428 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
429 if (ret) \
430 return -EINVAL; \
431 \
Dave Jones29464f22009-01-18 01:37:11 -0500432 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (ret != 1) \
434 return -EINVAL; \
435 \
Viresh Kumar037ce832013-10-02 14:13:16 +0530436 ret = cpufreq_set_policy(policy, &new_policy); \
Thomas Renninger7970e082006-04-13 15:14:04 +0200437 policy->user_policy.object = policy->object; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 \
439 return ret ? ret : count; \
440}
441
Dave Jones29464f22009-01-18 01:37:11 -0500442store_one(scaling_min_freq, min);
443store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445/**
446 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
447 */
Dave Jones905d77c2008-03-05 14:28:32 -0500448static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
449 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800451 unsigned int cur_freq = __cpufreq_get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (!cur_freq)
453 return sprintf(buf, "<unknown>");
454 return sprintf(buf, "%u\n", cur_freq);
455}
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457/**
458 * show_scaling_governor - show the current policy for the specified CPU
459 */
Dave Jones905d77c2008-03-05 14:28:32 -0500460static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Dave Jones29464f22009-01-18 01:37:11 -0500462 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return sprintf(buf, "powersave\n");
464 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
465 return sprintf(buf, "performance\n");
466 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200467 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500468 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return -EINVAL;
470}
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472/**
473 * store_scaling_governor - store policy for the specified CPU
474 */
Dave Jones905d77c2008-03-05 14:28:32 -0500475static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
476 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530478 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 char str_governor[16];
480 struct cpufreq_policy new_policy;
481
482 ret = cpufreq_get_policy(&new_policy, policy->cpu);
483 if (ret)
484 return ret;
485
Dave Jones29464f22009-01-18 01:37:11 -0500486 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (ret != 1)
488 return -EINVAL;
489
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530490 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
491 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return -EINVAL;
493
Viresh Kumar037ce832013-10-02 14:13:16 +0530494 ret = cpufreq_set_policy(policy, &new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200495
496 policy->user_policy.policy = policy->policy;
497 policy->user_policy.governor = policy->governor;
Thomas Renninger7970e082006-04-13 15:14:04 +0200498
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530499 if (ret)
500 return ret;
501 else
502 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
505/**
506 * show_scaling_driver - show the cpufreq driver currently loaded
507 */
Dave Jones905d77c2008-03-05 14:28:32 -0500508static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200510 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
513/**
514 * show_scaling_available_governors - show the available CPUfreq governors
515 */
Dave Jones905d77c2008-03-05 14:28:32 -0500516static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
517 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
519 ssize_t i = 0;
520 struct cpufreq_governor *t;
521
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200522 if (!cpufreq_driver->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 i += sprintf(buf, "performance powersave");
524 goto out;
525 }
526
527 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
Dave Jones29464f22009-01-18 01:37:11 -0500528 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
529 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200531 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500533out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 i += sprintf(&buf[i], "\n");
535 return i;
536}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700537
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800538ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
540 ssize_t i = 0;
541 unsigned int cpu;
542
Rusty Russell835481d2009-01-04 05:18:06 -0800543 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (i)
545 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
546 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
547 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500548 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
550 i += sprintf(&buf[i], "\n");
551 return i;
552}
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800553EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700555/**
556 * show_related_cpus - show the CPUs affected by each transition even if
557 * hw coordination is in use
558 */
559static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
560{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800561 return cpufreq_show_cpus(policy->related_cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700562}
563
564/**
565 * show_affected_cpus - show the CPUs affected by each transition
566 */
567static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
568{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800569 return cpufreq_show_cpus(policy->cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700570}
571
Venki Pallipadi9e769882007-10-26 10:18:21 -0700572static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500573 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700574{
575 unsigned int freq = 0;
576 unsigned int ret;
577
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700578 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700579 return -EINVAL;
580
581 ret = sscanf(buf, "%u", &freq);
582 if (ret != 1)
583 return -EINVAL;
584
585 policy->governor->store_setspeed(policy, freq);
586
587 return count;
588}
589
590static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
591{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700592 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700593 return sprintf(buf, "<unsupported>\n");
594
595 return policy->governor->show_setspeed(policy, buf);
596}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Thomas Renningere2f74f32009-11-19 12:31:01 +0100598/**
viresh kumar8bf1ac722012-10-23 01:23:33 +0200599 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100600 */
601static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
602{
603 unsigned int limit;
604 int ret;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200605 if (cpufreq_driver->bios_limit) {
606 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
Thomas Renningere2f74f32009-11-19 12:31:01 +0100607 if (!ret)
608 return sprintf(buf, "%u\n", limit);
609 }
610 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
611}
612
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200613cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
614cpufreq_freq_attr_ro(cpuinfo_min_freq);
615cpufreq_freq_attr_ro(cpuinfo_max_freq);
616cpufreq_freq_attr_ro(cpuinfo_transition_latency);
617cpufreq_freq_attr_ro(scaling_available_governors);
618cpufreq_freq_attr_ro(scaling_driver);
619cpufreq_freq_attr_ro(scaling_cur_freq);
620cpufreq_freq_attr_ro(bios_limit);
621cpufreq_freq_attr_ro(related_cpus);
622cpufreq_freq_attr_ro(affected_cpus);
623cpufreq_freq_attr_rw(scaling_min_freq);
624cpufreq_freq_attr_rw(scaling_max_freq);
625cpufreq_freq_attr_rw(scaling_governor);
626cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Dave Jones905d77c2008-03-05 14:28:32 -0500628static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 &cpuinfo_min_freq.attr,
630 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100631 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 &scaling_min_freq.attr,
633 &scaling_max_freq.attr,
634 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700635 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 &scaling_governor.attr,
637 &scaling_driver.attr,
638 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700639 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 NULL
641};
642
Dave Jones29464f22009-01-18 01:37:11 -0500643#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
644#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Dave Jones29464f22009-01-18 01:37:11 -0500646static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Dave Jones905d77c2008-03-05 14:28:32 -0500648 struct cpufreq_policy *policy = to_policy(kobj);
649 struct freq_attr *fattr = to_attr(attr);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530650 ssize_t ret;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530651
652 if (!down_read_trylock(&cpufreq_rwsem))
Viresh Kumar1b750e32013-10-02 14:13:09 +0530653 return -EINVAL;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800654
Viresh Kumar1b750e32013-10-02 14:13:09 +0530655 lock_policy_rwsem_read(policy->cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800656
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530657 if (fattr->show)
658 ret = fattr->show(policy, buf);
659 else
660 ret = -EIO;
661
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800662 unlock_policy_rwsem_read(policy->cpu);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530663 up_read(&cpufreq_rwsem);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return ret;
666}
667
Dave Jones905d77c2008-03-05 14:28:32 -0500668static ssize_t store(struct kobject *kobj, struct attribute *attr,
669 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Dave Jones905d77c2008-03-05 14:28:32 -0500671 struct cpufreq_policy *policy = to_policy(kobj);
672 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500673 ssize_t ret = -EINVAL;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530674
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530675 get_online_cpus();
676
677 if (!cpu_online(policy->cpu))
678 goto unlock;
679
Viresh Kumar6eed9402013-08-06 22:53:11 +0530680 if (!down_read_trylock(&cpufreq_rwsem))
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530681 goto unlock;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800682
Viresh Kumar1b750e32013-10-02 14:13:09 +0530683 lock_policy_rwsem_write(policy->cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800684
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530685 if (fattr->store)
686 ret = fattr->store(policy, buf, count);
687 else
688 ret = -EIO;
689
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800690 unlock_policy_rwsem_write(policy->cpu);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530691
Viresh Kumar6eed9402013-08-06 22:53:11 +0530692 up_read(&cpufreq_rwsem);
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530693unlock:
694 put_online_cpus();
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return ret;
697}
698
Dave Jones905d77c2008-03-05 14:28:32 -0500699static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Dave Jones905d77c2008-03-05 14:28:32 -0500701 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200702 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 complete(&policy->kobj_unregister);
704}
705
Emese Revfy52cf25d2010-01-19 02:58:23 +0100706static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 .show = show,
708 .store = store,
709};
710
711static struct kobj_type ktype_cpufreq = {
712 .sysfs_ops = &sysfs_ops,
713 .default_attrs = default_attrs,
714 .release = cpufreq_sysfs_release,
715};
716
Viresh Kumar2361be22013-05-17 16:09:09 +0530717struct kobject *cpufreq_global_kobject;
718EXPORT_SYMBOL(cpufreq_global_kobject);
719
720static int cpufreq_global_kobject_usage;
721
722int cpufreq_get_global_kobject(void)
723{
724 if (!cpufreq_global_kobject_usage++)
725 return kobject_add(cpufreq_global_kobject,
726 &cpu_subsys.dev_root->kobj, "%s", "cpufreq");
727
728 return 0;
729}
730EXPORT_SYMBOL(cpufreq_get_global_kobject);
731
732void cpufreq_put_global_kobject(void)
733{
734 if (!--cpufreq_global_kobject_usage)
735 kobject_del(cpufreq_global_kobject);
736}
737EXPORT_SYMBOL(cpufreq_put_global_kobject);
738
739int cpufreq_sysfs_create_file(const struct attribute *attr)
740{
741 int ret = cpufreq_get_global_kobject();
742
743 if (!ret) {
744 ret = sysfs_create_file(cpufreq_global_kobject, attr);
745 if (ret)
746 cpufreq_put_global_kobject();
747 }
748
749 return ret;
750}
751EXPORT_SYMBOL(cpufreq_sysfs_create_file);
752
753void cpufreq_sysfs_remove_file(const struct attribute *attr)
754{
755 sysfs_remove_file(cpufreq_global_kobject, attr);
756 cpufreq_put_global_kobject();
757}
758EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
759
Dave Jones19d6f7e2009-07-08 17:35:39 -0400760/* symlink affected CPUs */
Viresh Kumar308b60e2013-07-31 14:35:14 +0200761static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400762{
763 unsigned int j;
764 int ret = 0;
765
766 for_each_cpu(j, policy->cpus) {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800767 struct device *cpu_dev;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400768
Viresh Kumar308b60e2013-07-31 14:35:14 +0200769 if (j == policy->cpu)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400770 continue;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400771
Viresh Kumare8fdde12013-07-31 14:31:33 +0200772 pr_debug("Adding link for CPU: %u\n", j);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800773 cpu_dev = get_cpu_device(j);
774 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Dave Jones19d6f7e2009-07-08 17:35:39 -0400775 "cpufreq");
Rafael J. Wysocki71c34612013-08-04 01:19:34 +0200776 if (ret)
777 break;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400778 }
779 return ret;
780}
781
Viresh Kumar308b60e2013-07-31 14:35:14 +0200782static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800783 struct device *dev)
Dave Jones909a6942009-07-08 18:05:42 -0400784{
785 struct freq_attr **drv_attr;
Dave Jones909a6942009-07-08 18:05:42 -0400786 int ret = 0;
Dave Jones909a6942009-07-08 18:05:42 -0400787
788 /* prepare interface data */
789 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800790 &dev->kobj, "cpufreq");
Dave Jones909a6942009-07-08 18:05:42 -0400791 if (ret)
792 return ret;
793
794 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200795 drv_attr = cpufreq_driver->attr;
Dave Jones909a6942009-07-08 18:05:42 -0400796 while ((drv_attr) && (*drv_attr)) {
797 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
798 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200799 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400800 drv_attr++;
801 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200802 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -0400803 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
804 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200805 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400806 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200807 if (cpufreq_driver->target) {
Dave Jones909a6942009-07-08 18:05:42 -0400808 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
809 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200810 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400811 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200812 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +0100813 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
814 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200815 goto err_out_kobj_put;
Thomas Renningere2f74f32009-11-19 12:31:01 +0100816 }
Dave Jones909a6942009-07-08 18:05:42 -0400817
Viresh Kumar308b60e2013-07-31 14:35:14 +0200818 ret = cpufreq_add_dev_symlink(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400819 if (ret)
820 goto err_out_kobj_put;
821
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530822 return ret;
823
824err_out_kobj_put:
825 kobject_put(&policy->kobj);
826 wait_for_completion(&policy->kobj_unregister);
827 return ret;
828}
829
830static void cpufreq_init_policy(struct cpufreq_policy *policy)
831{
832 struct cpufreq_policy new_policy;
833 int ret = 0;
834
Viresh Kumard5b73cd2013-08-06 22:53:06 +0530835 memcpy(&new_policy, policy, sizeof(*policy));
Viresh Kumar037ce832013-10-02 14:13:16 +0530836 /* assure that the starting sequence is run in cpufreq_set_policy */
Dave Jonesecf7e462009-07-08 18:48:47 -0400837 policy->governor = NULL;
838
839 /* set default policy */
Viresh Kumar037ce832013-10-02 14:13:16 +0530840 ret = cpufreq_set_policy(policy, &new_policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400841 policy->user_policy.policy = policy->policy;
842 policy->user_policy.governor = policy->governor;
843
844 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200845 pr_debug("setting policy failed\n");
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200846 if (cpufreq_driver->exit)
847 cpufreq_driver->exit(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400848 }
Dave Jones909a6942009-07-08 18:05:42 -0400849}
850
Viresh Kumarfcf80582013-01-29 14:39:08 +0000851#ifdef CONFIG_HOTPLUG_CPU
Viresh Kumard8d3b472013-08-04 01:20:07 +0200852static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,
853 unsigned int cpu, struct device *dev,
854 bool frozen)
Viresh Kumarfcf80582013-01-29 14:39:08 +0000855{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200856 int ret = 0, has_target = !!cpufreq_driver->target;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000857 unsigned long flags;
858
Viresh Kumar3de9bde2013-08-06 22:53:13 +0530859 if (has_target) {
860 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
861 if (ret) {
862 pr_err("%s: Failed to stop governor\n", __func__);
863 return ret;
864 }
865 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000866
Viresh Kumard8d3b472013-08-04 01:20:07 +0200867 lock_policy_rwsem_write(policy->cpu);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530868
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000869 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530870
Viresh Kumarfcf80582013-01-29 14:39:08 +0000871 cpumask_set_cpu(cpu, policy->cpus);
872 per_cpu(cpufreq_cpu_data, cpu) = policy;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000873 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000874
Viresh Kumard8d3b472013-08-04 01:20:07 +0200875 unlock_policy_rwsem_write(policy->cpu);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530876
Viresh Kumar820c6ca2013-04-22 00:48:03 +0200877 if (has_target) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +0530878 if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
879 (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
880 pr_err("%s: Failed to start governor\n", __func__);
881 return ret;
882 }
Viresh Kumar820c6ca2013-04-22 00:48:03 +0200883 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000884
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +0530885 /* Don't touch sysfs links during light-weight init */
Rafael J. Wysocki71c34612013-08-04 01:19:34 +0200886 if (!frozen)
887 ret = sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
Viresh Kumarfcf80582013-01-29 14:39:08 +0000888
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +0530889 return ret;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000890}
891#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530893static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu)
894{
895 struct cpufreq_policy *policy;
896 unsigned long flags;
897
Lan Tianyu44871c92013-09-11 15:05:05 +0800898 read_lock_irqsave(&cpufreq_driver_lock, flags);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530899
900 policy = per_cpu(cpufreq_cpu_data_fallback, cpu);
901
Lan Tianyu44871c92013-09-11 15:05:05 +0800902 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530903
904 return policy;
905}
906
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +0530907static struct cpufreq_policy *cpufreq_policy_alloc(void)
908{
909 struct cpufreq_policy *policy;
910
911 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
912 if (!policy)
913 return NULL;
914
915 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
916 goto err_free_policy;
917
918 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
919 goto err_free_cpumask;
920
Lukasz Majewskic88a1f82013-08-06 22:53:08 +0530921 INIT_LIST_HEAD(&policy->policy_list);
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +0530922 return policy;
923
924err_free_cpumask:
925 free_cpumask_var(policy->cpus);
926err_free_policy:
927 kfree(policy);
928
929 return NULL;
930}
931
932static void cpufreq_policy_free(struct cpufreq_policy *policy)
933{
934 free_cpumask_var(policy->related_cpus);
935 free_cpumask_var(policy->cpus);
936 kfree(policy);
937}
938
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +0530939static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
940{
Srivatsa S. Bhatcb38ed52013-09-12 01:43:42 +0530941 if (cpu == policy->cpu)
942 return;
943
Viresh Kumar8efd5762013-09-17 10:22:11 +0530944 /*
945 * Take direct locks as lock_policy_rwsem_write wouldn't work here.
946 * Also lock for last cpu is enough here as contention will happen only
947 * after policy->cpu is changed and after it is changed, other threads
948 * will try to acquire lock for new cpu. And policy is already updated
949 * by then.
950 */
951 down_write(&per_cpu(cpu_policy_rwsem, policy->cpu));
952
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +0530953 policy->last_cpu = policy->cpu;
954 policy->cpu = cpu;
955
Viresh Kumar8efd5762013-09-17 10:22:11 +0530956 up_write(&per_cpu(cpu_policy_rwsem, policy->last_cpu));
957
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +0530958#ifdef CONFIG_CPU_FREQ_TABLE
959 cpufreq_frequency_table_update_policy_cpu(policy);
960#endif
961 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
962 CPUFREQ_UPDATE_POLICY_CPU, policy);
963}
964
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +0530965static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif,
966 bool frozen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Viresh Kumarfcf80582013-01-29 14:39:08 +0000968 unsigned int j, cpu = dev->id;
Viresh Kumar65922462013-02-07 10:56:03 +0530969 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 unsigned long flags;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500972#ifdef CONFIG_HOTPLUG_CPU
Viresh Kumar1b274292013-08-20 12:08:26 +0530973 struct cpufreq_policy *tpolicy;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000974 struct cpufreq_governor *gov;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500975#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Ashok Rajc32b6b82005-10-30 14:59:54 -0800977 if (cpu_is_offline(cpu))
978 return 0;
979
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200980 pr_debug("adding CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982#ifdef CONFIG_SMP
983 /* check whether a different CPU already registered this
984 * CPU because it is in the same boat. */
985 policy = cpufreq_cpu_get(cpu);
986 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -0500987 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return 0;
989 }
Li Zhong5025d622013-08-21 01:31:08 +0200990#endif
Viresh Kumarfcf80582013-01-29 14:39:08 +0000991
Viresh Kumar6eed9402013-08-06 22:53:11 +0530992 if (!down_read_trylock(&cpufreq_rwsem))
993 return 0;
994
Viresh Kumarfcf80582013-01-29 14:39:08 +0000995#ifdef CONFIG_HOTPLUG_CPU
996 /* Check if this cpu was hot-unplugged earlier and has siblings */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000997 read_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar1b274292013-08-20 12:08:26 +0530998 list_for_each_entry(tpolicy, &cpufreq_policy_list, policy_list) {
999 if (cpumask_test_cpu(cpu, tpolicy->related_cpus)) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001000 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar1b274292013-08-20 12:08:26 +05301001 ret = cpufreq_add_policy_cpu(tpolicy, cpu, dev, frozen);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301002 up_read(&cpufreq_rwsem);
1003 return ret;
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301004 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001005 }
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001006 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +00001007#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301009 if (frozen)
1010 /* Restore the saved policy when doing light-weight init */
1011 policy = cpufreq_policy_restore(cpu);
1012 else
1013 policy = cpufreq_policy_alloc();
1014
Dave Jones059019a2009-07-08 16:30:03 -04001015 if (!policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 goto nomem_out;
Dave Jones059019a2009-07-08 16:30:03 -04001017
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301018
1019 /*
1020 * In the resume path, since we restore a saved policy, the assignment
1021 * to policy->cpu is like an update of the existing policy, rather than
1022 * the creation of a brand new one. So we need to perform this update
1023 * by invoking update_policy_cpu().
1024 */
1025 if (frozen && cpu != policy->cpu)
1026 update_policy_cpu(policy, cpu);
1027 else
1028 policy->cpu = cpu;
1029
Viresh Kumar65922462013-02-07 10:56:03 +05301030 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Rusty Russell835481d2009-01-04 05:18:06 -08001031 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 init_completion(&policy->kobj_unregister);
David Howells65f27f32006-11-22 14:55:48 +00001034 INIT_WORK(&policy->update, handle_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 /* call driver. From then on the cpufreq must be able
1037 * to accept all calls to ->verify and ->setpolicy for this CPU
1038 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001039 ret = cpufreq_driver->init(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001041 pr_debug("initialization failed\n");
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301042 goto err_set_policy_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 }
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001044
Viresh Kumarfcf80582013-01-29 14:39:08 +00001045 /* related cpus should atleast have policy->cpus */
1046 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
1047
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001048 /*
1049 * affected cpus must always be the one, which are online. We aren't
1050 * managing offline cpus here.
1051 */
1052 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1053
Mike Chan187d9f42008-12-04 12:19:17 -08001054 policy->user_policy.min = policy->min;
1055 policy->user_policy.max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Thomas Renningera1531ac2008-07-29 22:32:58 -07001057 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1058 CPUFREQ_START, policy);
1059
Viresh Kumarfcf80582013-01-29 14:39:08 +00001060#ifdef CONFIG_HOTPLUG_CPU
1061 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
1062 if (gov) {
1063 policy->governor = gov;
1064 pr_debug("Restoring governor %s for cpu %d\n",
1065 policy->governor->name, cpu);
Thomas Renninger4bfa0422009-07-24 15:25:03 +02001066 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001067#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301069 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar474deff2013-08-20 12:08:25 +05301070 for_each_cpu(j, policy->cpus)
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301071 per_cpu(cpufreq_cpu_data, j) = policy;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301072 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1073
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301074 if (!frozen) {
Viresh Kumar308b60e2013-07-31 14:35:14 +02001075 ret = cpufreq_add_dev_interface(policy, dev);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301076 if (ret)
1077 goto err_out_unregister;
1078 }
Dave Jones8ff69732006-03-05 03:37:23 -05001079
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301080 write_lock_irqsave(&cpufreq_driver_lock, flags);
1081 list_add(&policy->policy_list, &cpufreq_policy_list);
1082 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1083
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301084 cpufreq_init_policy(policy);
1085
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001086 kobject_uevent(&policy->kobj, KOBJ_ADD);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301087 up_read(&cpufreq_rwsem);
1088
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001089 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 return 0;
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093err_out_unregister:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001094 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar474deff2013-08-20 12:08:25 +05301095 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -07001096 per_cpu(cpufreq_cpu_data, j) = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001097 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301099err_set_policy_cpu:
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301100 cpufreq_policy_free(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101nomem_out:
Viresh Kumar6eed9402013-08-06 22:53:11 +05301102 up_read(&cpufreq_rwsem);
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 return ret;
1105}
1106
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301107/**
1108 * cpufreq_add_dev - add a CPU device
1109 *
1110 * Adds the cpufreq interface for a CPU device.
1111 *
1112 * The Oracle says: try running cpufreq registration/unregistration concurrently
1113 * with with cpu hotplugging and all hell will break loose. Tried to clean this
1114 * mess up, but more thorough testing is needed. - Mathieu
1115 */
1116static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1117{
1118 return __cpufreq_add_dev(dev, sif, false);
1119}
1120
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301121static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy *policy,
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301122 unsigned int old_cpu, bool frozen)
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301123{
1124 struct device *cpu_dev;
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301125 int ret;
1126
1127 /* first sibling now owns the new sysfs dir */
Viresh Kumar9c8f1ee2013-09-12 17:06:33 +05301128 cpu_dev = get_cpu_device(cpumask_any_but(policy->cpus, old_cpu));
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301129
1130 /* Don't touch sysfs files during light-weight tear-down */
1131 if (frozen)
1132 return cpu_dev->id;
1133
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301134 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301135 ret = kobject_move(&policy->kobj, &cpu_dev->kobj);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301136 if (ret) {
1137 pr_err("%s: Failed to move kobj: %d", __func__, ret);
1138
Viresh Kumar1b750e32013-10-02 14:13:09 +05301139 lock_policy_rwsem_write(old_cpu);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301140 cpumask_set_cpu(old_cpu, policy->cpus);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301141 unlock_policy_rwsem_write(old_cpu);
1142
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301143 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301144 "cpufreq");
1145
1146 return -EINVAL;
1147 }
1148
1149 return cpu_dev->id;
1150}
1151
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301152static int __cpufreq_remove_dev_prepare(struct device *dev,
1153 struct subsys_interface *sif,
1154 bool frozen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301156 unsigned int cpu = dev->id, cpus;
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301157 int new_cpu, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 unsigned long flags;
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301159 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001161 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001163 write_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301165 policy = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301167 /* Save the policy somewhere when doing a light-weight tear-down */
1168 if (frozen)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301169 per_cpu(cpufreq_cpu_data_fallback, cpu) = policy;
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301170
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001171 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301173 if (!policy) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001174 pr_debug("%s: No cpu_data found\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301178 if (cpufreq_driver->target) {
1179 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1180 if (ret) {
1181 pr_err("%s: Failed to stop governor\n", __func__);
1182 return ret;
1183 }
1184 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001185
Jacob Shin27ecddc2011-04-27 13:32:11 -05001186#ifdef CONFIG_HOTPLUG_CPU
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001187 if (!cpufreq_driver->setpolicy)
Dirk Brandewiefa69e332013-02-06 09:02:11 -08001188 strncpy(per_cpu(cpufreq_cpu_governor, cpu),
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301189 policy->governor->name, CPUFREQ_NAME_LEN);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001190#endif
1191
Viresh Kumar9c8f1ee2013-09-12 17:06:33 +05301192 lock_policy_rwsem_read(cpu);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301193 cpus = cpumask_weight(policy->cpus);
Viresh Kumar9c8f1ee2013-09-12 17:06:33 +05301194 unlock_policy_rwsem_read(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Srivatsa S. Bhat61173f22013-09-12 01:43:25 +05301196 if (cpu != policy->cpu) {
1197 if (!frozen)
1198 sysfs_remove_link(&dev->kobj, "cpufreq");
Viresh Kumar73bf0fc2013-02-05 22:21:14 +01001199 } else if (cpus > 1) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301200 new_cpu = cpufreq_nominate_new_policy_cpu(policy, cpu, frozen);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301201 if (new_cpu >= 0) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301202 update_policy_cpu(policy, new_cpu);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301203
1204 if (!frozen) {
Viresh Kumar75949c92013-10-02 14:13:13 +05301205 pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
1206 __func__, new_cpu, cpu);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301207 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001208 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001209 }
Venki Pallipadiec282972007-03-26 12:03:19 -07001210
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301211 return 0;
1212}
1213
1214static int __cpufreq_remove_dev_finish(struct device *dev,
1215 struct subsys_interface *sif,
1216 bool frozen)
1217{
1218 unsigned int cpu = dev->id, cpus;
1219 int ret;
1220 unsigned long flags;
1221 struct cpufreq_policy *policy;
1222 struct kobject *kobj;
1223 struct completion *cmp;
1224
1225 read_lock_irqsave(&cpufreq_driver_lock, flags);
1226 policy = per_cpu(cpufreq_cpu_data, cpu);
1227 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1228
1229 if (!policy) {
1230 pr_debug("%s: No cpu_data found\n", __func__);
1231 return -EINVAL;
1232 }
1233
Viresh Kumar1b750e32013-10-02 14:13:09 +05301234 lock_policy_rwsem_write(cpu);
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301235 cpus = cpumask_weight(policy->cpus);
Viresh Kumar9c8f1ee2013-09-12 17:06:33 +05301236
1237 if (cpus > 1)
1238 cpumask_clear_cpu(cpu, policy->cpus);
1239 unlock_policy_rwsem_write(cpu);
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301240
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001241 /* If cpu is last user of policy, free policy */
1242 if (cpus == 1) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301243 if (cpufreq_driver->target) {
1244 ret = __cpufreq_governor(policy,
1245 CPUFREQ_GOV_POLICY_EXIT);
1246 if (ret) {
1247 pr_err("%s: Failed to exit governor\n",
1248 __func__);
1249 return ret;
1250 }
Viresh Kumaredab2fb2013-08-20 12:08:22 +05301251 }
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001252
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301253 if (!frozen) {
1254 lock_policy_rwsem_read(cpu);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301255 kobj = &policy->kobj;
1256 cmp = &policy->kobj_unregister;
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301257 unlock_policy_rwsem_read(cpu);
1258 kobject_put(kobj);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001259
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301260 /*
1261 * We need to make sure that the underlying kobj is
1262 * actually not referenced anymore by anybody before we
1263 * proceed with unloading.
1264 */
1265 pr_debug("waiting for dropping of refcount\n");
1266 wait_for_completion(cmp);
1267 pr_debug("wait complete\n");
1268 }
1269
1270 /*
1271 * Perform the ->exit() even during light-weight tear-down,
1272 * since this is a core component, and is essential for the
1273 * subsequent light-weight ->init() to succeed.
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001274 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001275 if (cpufreq_driver->exit)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301276 cpufreq_driver->exit(policy);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001277
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301278 /* Remove policy from list of active policies */
1279 write_lock_irqsave(&cpufreq_driver_lock, flags);
1280 list_del(&policy->policy_list);
1281 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1282
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301283 if (!frozen)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301284 cpufreq_policy_free(policy);
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001285 } else {
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001286 if (cpufreq_driver->target) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301287 if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
1288 (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
1289 pr_err("%s: Failed to start governor\n",
1290 __func__);
1291 return ret;
1292 }
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001293 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Viresh Kumar474deff2013-08-20 12:08:25 +05301296 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return 0;
1298}
1299
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301300/**
Viresh Kumar27a862e2013-10-02 14:13:14 +05301301 * cpufreq_remove_dev - remove a CPU device
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301302 *
1303 * Removes the cpufreq interface for a CPU device.
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301304 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001305static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001306{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001307 unsigned int cpu = dev->id;
Viresh Kumar27a862e2013-10-02 14:13:14 +05301308 int ret;
Venki Pallipadiec282972007-03-26 12:03:19 -07001309
1310 if (cpu_is_offline(cpu))
1311 return 0;
1312
Viresh Kumar27a862e2013-10-02 14:13:14 +05301313 ret = __cpufreq_remove_dev_prepare(dev, sif, false);
1314
1315 if (!ret)
1316 ret = __cpufreq_remove_dev_finish(dev, sif, false);
1317
1318 return ret;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001319}
1320
David Howells65f27f32006-11-22 14:55:48 +00001321static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
David Howells65f27f32006-11-22 14:55:48 +00001323 struct cpufreq_policy *policy =
1324 container_of(work, struct cpufreq_policy, update);
1325 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001326 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 cpufreq_update_policy(cpu);
1328}
1329
1330/**
Viresh Kumarbb176f72013-06-19 14:19:33 +05301331 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1332 * in deep trouble.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 * @cpu: cpu number
1334 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1335 * @new_freq: CPU frequency the CPU actually runs at
1336 *
Dave Jones29464f22009-01-18 01:37:11 -05001337 * We adjust to current frequency first, and need to clean up later.
1338 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301340static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1341 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301343 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301345 unsigned long flags;
1346
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001347 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 freqs.old = old_freq;
1351 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301352
1353 read_lock_irqsave(&cpufreq_driver_lock, flags);
1354 policy = per_cpu(cpufreq_cpu_data, cpu);
1355 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1356
1357 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1358 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359}
1360
Dave Jones32ee8c32006-02-28 00:43:23 -05001361/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301362 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001363 * @cpu: CPU number
1364 *
1365 * This is the last known freq, without actually getting it from the driver.
1366 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1367 */
1368unsigned int cpufreq_quick_get(unsigned int cpu)
1369{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001370 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301371 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001372
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001373 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1374 return cpufreq_driver->get(cpu);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001375
1376 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001377 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301378 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001379 cpufreq_cpu_put(policy);
1380 }
1381
Dave Jones4d34a672008-02-07 16:33:49 -05001382 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001383}
1384EXPORT_SYMBOL(cpufreq_quick_get);
1385
Jesse Barnes3d737102011-06-28 10:59:12 -07001386/**
1387 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1388 * @cpu: CPU number
1389 *
1390 * Just return the max possible frequency for a given CPU.
1391 */
1392unsigned int cpufreq_quick_get_max(unsigned int cpu)
1393{
1394 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1395 unsigned int ret_freq = 0;
1396
1397 if (policy) {
1398 ret_freq = policy->max;
1399 cpufreq_cpu_put(policy);
1400 }
1401
1402 return ret_freq;
1403}
1404EXPORT_SYMBOL(cpufreq_quick_get_max);
1405
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001406static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001408 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301409 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001411 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001412 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001414 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301416 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001417 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301418 /* verify no discrepancy between actual and
1419 saved value exists */
1420 if (unlikely(ret_freq != policy->cur)) {
1421 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 schedule_work(&policy->update);
1423 }
1424 }
1425
Dave Jones4d34a672008-02-07 16:33:49 -05001426 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001427}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001429/**
1430 * cpufreq_get - get the current CPU frequency (in kHz)
1431 * @cpu: CPU number
1432 *
1433 * Get the CPU current (static) CPU frequency
1434 */
1435unsigned int cpufreq_get(unsigned int cpu)
1436{
1437 unsigned int ret_freq = 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001438
Viresh Kumar26ca8692013-09-20 22:37:31 +05301439 if (cpufreq_disabled() || !cpufreq_driver)
1440 return -ENOENT;
1441
Viresh Kumar6eed9402013-08-06 22:53:11 +05301442 if (!down_read_trylock(&cpufreq_rwsem))
1443 return 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001444
Viresh Kumar1b750e32013-10-02 14:13:09 +05301445 lock_policy_rwsem_read(cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001446
1447 ret_freq = __cpufreq_get(cpu);
1448
1449 unlock_policy_rwsem_read(cpu);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301450 up_read(&cpufreq_rwsem);
1451
Dave Jones4d34a672008-02-07 16:33:49 -05001452 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453}
1454EXPORT_SYMBOL(cpufreq_get);
1455
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001456static struct subsys_interface cpufreq_interface = {
1457 .name = "cpufreq",
1458 .subsys = &cpu_subsys,
1459 .add_dev = cpufreq_add_dev,
1460 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001461};
1462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001464 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1465 *
1466 * This function is only executed for the boot processor. The other CPUs
1467 * have been put offline by means of CPU hotplug.
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001468 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001469static int cpufreq_bp_suspend(void)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001470{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301471 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001472
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001473 int cpu = smp_processor_id();
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301474 struct cpufreq_policy *policy;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001475
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001476 pr_debug("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001477
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001478 /* If there's no policy for the boot CPU, we have nothing to do. */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301479 policy = cpufreq_cpu_get(cpu);
1480 if (!policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001481 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001482
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001483 if (cpufreq_driver->suspend) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301484 ret = cpufreq_driver->suspend(policy);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001485 if (ret)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001486 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301487 "step on CPU %u\n", policy->cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001488 }
1489
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301490 cpufreq_cpu_put(policy);
Dave Jonesc9060492008-02-07 16:32:18 -05001491 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001492}
1493
1494/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001495 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 *
1497 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001498 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1499 * restored. It will verify that the current freq is in sync with
1500 * what we believe it to be. This is a bit later than when it
1501 * should be, but nonethteless it's better than calling
1502 * cpufreq_driver->get() here which might re-enable interrupts...
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001503 *
1504 * This function is only executed for the boot CPU. The other CPUs have not
1505 * been turned on yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001507static void cpufreq_bp_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301509 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001510
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001511 int cpu = smp_processor_id();
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301512 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001514 pr_debug("resuming cpu %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001516 /* If there's no policy for the boot CPU, we have nothing to do. */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301517 policy = cpufreq_cpu_get(cpu);
1518 if (!policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001519 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001521 if (cpufreq_driver->resume) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301522 ret = cpufreq_driver->resume(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 if (ret) {
1524 printk(KERN_ERR "cpufreq: resume failed in ->resume "
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301525 "step on CPU %u\n", policy->cpu);
Dave Jonesc9060492008-02-07 16:32:18 -05001526 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
1528 }
1529
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301530 schedule_work(&policy->update);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001531
Dave Jonesc9060492008-02-07 16:32:18 -05001532fail:
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301533 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534}
1535
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001536static struct syscore_ops cpufreq_syscore_ops = {
1537 .suspend = cpufreq_bp_suspend,
1538 .resume = cpufreq_bp_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539};
1540
Borislav Petkov9d950462013-01-20 10:24:28 +00001541/**
1542 * cpufreq_get_current_driver - return current driver's name
1543 *
1544 * Return the name string of the currently loaded cpufreq driver
1545 * or NULL, if none.
1546 */
1547const char *cpufreq_get_current_driver(void)
1548{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001549 if (cpufreq_driver)
1550 return cpufreq_driver->name;
1551
1552 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001553}
1554EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
1556/*********************************************************************
1557 * NOTIFIER LISTS INTERFACE *
1558 *********************************************************************/
1559
1560/**
1561 * cpufreq_register_notifier - register a driver with cpufreq
1562 * @nb: notifier function to register
1563 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1564 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001565 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 * are notified about clock rate changes (once before and once after
1567 * the transition), or a list of drivers that are notified about
1568 * changes in cpufreq policy.
1569 *
1570 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001571 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 */
1573int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1574{
1575 int ret;
1576
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001577 if (cpufreq_disabled())
1578 return -EINVAL;
1579
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001580 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 switch (list) {
1583 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001584 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001585 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 break;
1587 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001588 ret = blocking_notifier_chain_register(
1589 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 break;
1591 default:
1592 ret = -EINVAL;
1593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
1595 return ret;
1596}
1597EXPORT_SYMBOL(cpufreq_register_notifier);
1598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599/**
1600 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1601 * @nb: notifier block to be unregistered
Viresh Kumarbb176f72013-06-19 14:19:33 +05301602 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 *
1604 * Remove a driver from the CPU frequency notifier list.
1605 *
1606 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001607 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 */
1609int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1610{
1611 int ret;
1612
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001613 if (cpufreq_disabled())
1614 return -EINVAL;
1615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 switch (list) {
1617 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001618 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001619 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 break;
1621 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001622 ret = blocking_notifier_chain_unregister(
1623 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 break;
1625 default:
1626 ret = -EINVAL;
1627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
1629 return ret;
1630}
1631EXPORT_SYMBOL(cpufreq_unregister_notifier);
1632
1633
1634/*********************************************************************
1635 * GOVERNORS *
1636 *********************************************************************/
1637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638int __cpufreq_driver_target(struct cpufreq_policy *policy,
1639 unsigned int target_freq,
1640 unsigned int relation)
1641{
1642 int retval = -EINVAL;
Viresh Kumar72499242012-10-31 01:28:21 +01001643 unsigned int old_target_freq = target_freq;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001644
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001645 if (cpufreq_disabled())
1646 return -ENODEV;
1647
Viresh Kumar72499242012-10-31 01:28:21 +01001648 /* Make sure that target_freq is within supported range */
1649 if (target_freq > policy->max)
1650 target_freq = policy->max;
1651 if (target_freq < policy->min)
1652 target_freq = policy->min;
1653
1654 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1655 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001656
1657 if (target_freq == policy->cur)
1658 return 0;
1659
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001660 if (cpufreq_driver->target)
1661 retval = cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 return retval;
1664}
1665EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667int cpufreq_driver_target(struct cpufreq_policy *policy,
1668 unsigned int target_freq,
1669 unsigned int relation)
1670{
Julia Lawallf1829e42008-07-25 22:44:53 +02001671 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Viresh Kumar1b750e32013-10-02 14:13:09 +05301673 lock_policy_rwsem_write(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675 ret = __cpufreq_driver_target(policy, target_freq, relation);
1676
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001677 unlock_policy_rwsem_write(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 return ret;
1680}
1681EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1682
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001683/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001684 * when "event" is CPUFREQ_GOV_LIMITS
1685 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301687static int __cpufreq_governor(struct cpufreq_policy *policy,
1688 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
Dave Jonescc993ca2005-07-28 09:43:56 -07001690 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001691
1692 /* Only must be defined when default governor is known to have latency
1693 restrictions, like e.g. conservative or ondemand.
1694 That this is the case is already ensured in Kconfig
1695 */
1696#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1697 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1698#else
1699 struct cpufreq_governor *gov = NULL;
1700#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001701
1702 if (policy->governor->max_transition_latency &&
1703 policy->cpuinfo.transition_latency >
1704 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07001705 if (!gov)
1706 return -EINVAL;
1707 else {
1708 printk(KERN_WARNING "%s governor failed, too long"
1709 " transition latency of HW, fallback"
1710 " to %s governor\n",
1711 policy->governor->name,
1712 gov->name);
1713 policy->governor = gov;
1714 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Viresh Kumarfe492f32013-08-06 22:53:10 +05301717 if (event == CPUFREQ_GOV_POLICY_INIT)
1718 if (!try_module_get(policy->governor->owner))
1719 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001721 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301722 policy->cpu, event);
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001723
1724 mutex_lock(&cpufreq_governor_lock);
Srivatsa S. Bhat56d07db2013-09-07 01:23:55 +05301725 if ((policy->governor_enabled && event == CPUFREQ_GOV_START)
Viresh Kumarf73d3932013-08-31 17:53:40 +05301726 || (!policy->governor_enabled
1727 && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) {
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001728 mutex_unlock(&cpufreq_governor_lock);
1729 return -EBUSY;
1730 }
1731
1732 if (event == CPUFREQ_GOV_STOP)
1733 policy->governor_enabled = false;
1734 else if (event == CPUFREQ_GOV_START)
1735 policy->governor_enabled = true;
1736
1737 mutex_unlock(&cpufreq_governor_lock);
1738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 ret = policy->governor->governor(policy, event);
1740
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001741 if (!ret) {
1742 if (event == CPUFREQ_GOV_POLICY_INIT)
1743 policy->governor->initialized++;
1744 else if (event == CPUFREQ_GOV_POLICY_EXIT)
1745 policy->governor->initialized--;
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001746 } else {
1747 /* Restore original values */
1748 mutex_lock(&cpufreq_governor_lock);
1749 if (event == CPUFREQ_GOV_STOP)
1750 policy->governor_enabled = true;
1751 else if (event == CPUFREQ_GOV_START)
1752 policy->governor_enabled = false;
1753 mutex_unlock(&cpufreq_governor_lock);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001754 }
Viresh Kumarb3940582013-02-01 05:42:58 +00001755
Viresh Kumarfe492f32013-08-06 22:53:10 +05301756 if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
1757 ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 module_put(policy->governor->owner);
1759
1760 return ret;
1761}
1762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763int cpufreq_register_governor(struct cpufreq_governor *governor)
1764{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001765 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
1767 if (!governor)
1768 return -EINVAL;
1769
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001770 if (cpufreq_disabled())
1771 return -ENODEV;
1772
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001773 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001774
Viresh Kumarb3940582013-02-01 05:42:58 +00001775 governor->initialized = 0;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001776 err = -EBUSY;
1777 if (__find_governor(governor->name) == NULL) {
1778 err = 0;
1779 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Dave Jones32ee8c32006-02-28 00:43:23 -05001782 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001783 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784}
1785EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1788{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001789#ifdef CONFIG_HOTPLUG_CPU
1790 int cpu;
1791#endif
1792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 if (!governor)
1794 return;
1795
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001796 if (cpufreq_disabled())
1797 return;
1798
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001799#ifdef CONFIG_HOTPLUG_CPU
1800 for_each_present_cpu(cpu) {
1801 if (cpu_online(cpu))
1802 continue;
1803 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1804 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1805 }
1806#endif
1807
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001808 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001810 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 return;
1812}
1813EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1814
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816/*********************************************************************
1817 * POLICY INTERFACE *
1818 *********************************************************************/
1819
1820/**
1821 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05001822 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1823 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 *
1825 * Reads the current cpufreq policy.
1826 */
1827int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1828{
1829 struct cpufreq_policy *cpu_policy;
1830 if (!policy)
1831 return -EINVAL;
1832
1833 cpu_policy = cpufreq_cpu_get(cpu);
1834 if (!cpu_policy)
1835 return -EINVAL;
1836
Viresh Kumard5b73cd2013-08-06 22:53:06 +05301837 memcpy(policy, cpu_policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
1839 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 return 0;
1841}
1842EXPORT_SYMBOL(cpufreq_get_policy);
1843
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001844/*
Viresh Kumar037ce832013-10-02 14:13:16 +05301845 * policy : current policy.
1846 * new_policy: policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001847 */
Viresh Kumar037ce832013-10-02 14:13:16 +05301848static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301849 struct cpufreq_policy *new_policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001851 int ret = 0, failed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301853 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", new_policy->cpu,
1854 new_policy->min, new_policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Viresh Kumard5b73cd2013-08-06 22:53:06 +05301856 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301858 if (new_policy->min > policy->max || new_policy->max < policy->min) {
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02001859 ret = -EINVAL;
1860 goto error_out;
1861 }
1862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 /* verify the cpu speed can be set within this limit */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301864 ret = cpufreq_driver->verify(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 if (ret)
1866 goto error_out;
1867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001869 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301870 CPUFREQ_ADJUST, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001873 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301874 CPUFREQ_INCOMPATIBLE, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Viresh Kumarbb176f72013-06-19 14:19:33 +05301876 /*
1877 * verify the cpu speed can be set within this limit, which might be
1878 * different to the first one
1879 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301880 ret = cpufreq_driver->verify(new_policy);
Alan Sterne041c682006-03-27 01:16:30 -08001881 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
1884 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001885 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301886 CPUFREQ_NOTIFY, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301888 policy->min = new_policy->min;
1889 policy->max = new_policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001891 pr_debug("new min and max freqs are %u - %u kHz\n",
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301892 policy->min, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001894 if (cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301895 policy->policy = new_policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001896 pr_debug("setting range\n");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301897 ret = cpufreq_driver->setpolicy(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 } else {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301899 if (new_policy->governor != policy->governor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 /* save old, working values */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301901 struct cpufreq_governor *old_gov = policy->governor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001903 pr_debug("governor switch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
1905 /* end old governor */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301906 if (policy->governor) {
1907 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1908 unlock_policy_rwsem_write(new_policy->cpu);
1909 __cpufreq_governor(policy,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001910 CPUFREQ_GOV_POLICY_EXIT);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301911 lock_policy_rwsem_write(new_policy->cpu);
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
1914 /* start new governor */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301915 policy->governor = new_policy->governor;
1916 if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
1917 if (!__cpufreq_governor(policy, CPUFREQ_GOV_START)) {
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001918 failed = 0;
Viresh Kumar955ef482013-05-16 05:09:58 +00001919 } else {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301920 unlock_policy_rwsem_write(new_policy->cpu);
1921 __cpufreq_governor(policy,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001922 CPUFREQ_GOV_POLICY_EXIT);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301923 lock_policy_rwsem_write(new_policy->cpu);
Viresh Kumar955ef482013-05-16 05:09:58 +00001924 }
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001925 }
1926
1927 if (failed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 /* new governor failed, so re-start old one */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001929 pr_debug("starting governor %s failed\n",
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301930 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 if (old_gov) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301932 policy->governor = old_gov;
1933 __cpufreq_governor(policy,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001934 CPUFREQ_GOV_POLICY_INIT);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301935 __cpufreq_governor(policy,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301936 CPUFREQ_GOV_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 }
1938 ret = -EINVAL;
1939 goto error_out;
1940 }
1941 /* might be a policy change, too, so fall through */
1942 }
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001943 pr_debug("governor: change or update limits\n");
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301944 ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 }
1946
Dave Jones7d5e3502006-02-02 17:03:42 -05001947error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 return ret;
1949}
1950
1951/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1953 * @cpu: CPU which shall be re-evaluated
1954 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001955 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 * at different times.
1957 */
1958int cpufreq_update_policy(unsigned int cpu)
1959{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301960 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1961 struct cpufreq_policy new_policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02001962 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301964 if (!policy) {
Julia Lawallf1829e42008-07-25 22:44:53 +02001965 ret = -ENODEV;
1966 goto no_policy;
1967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Viresh Kumar1b750e32013-10-02 14:13:09 +05301969 lock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001971 pr_debug("updating policy for CPU %u\n", cpu);
Viresh Kumard5b73cd2013-08-06 22:53:06 +05301972 memcpy(&new_policy, policy, sizeof(*policy));
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301973 new_policy.min = policy->user_policy.min;
1974 new_policy.max = policy->user_policy.max;
1975 new_policy.policy = policy->user_policy.policy;
1976 new_policy.governor = policy->user_policy.governor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Viresh Kumarbb176f72013-06-19 14:19:33 +05301978 /*
1979 * BIOS might change freq behind our back
1980 * -> ask driver for current freq and notify governors about a change
1981 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001982 if (cpufreq_driver->get) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301983 new_policy.cur = cpufreq_driver->get(cpu);
1984 if (!policy->cur) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001985 pr_debug("Driver did not initialize current freq");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301986 policy->cur = new_policy.cur;
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001987 } else {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301988 if (policy->cur != new_policy.cur && cpufreq_driver->target)
1989 cpufreq_out_of_sync(cpu, policy->cur,
1990 new_policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001991 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01001992 }
1993
Viresh Kumar037ce832013-10-02 14:13:16 +05301994 ret = cpufreq_set_policy(policy, &new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001996 unlock_policy_rwsem_write(cpu);
1997
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301998 cpufreq_cpu_put(policy);
Julia Lawallf1829e42008-07-25 22:44:53 +02001999no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 return ret;
2001}
2002EXPORT_SYMBOL(cpufreq_update_policy);
2003
Paul Gortmaker27609842013-06-19 13:54:04 -04002004static int cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002005 unsigned long action, void *hcpu)
2006{
2007 unsigned int cpu = (unsigned long)hcpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002008 struct device *dev;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302009 bool frozen = false;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002010
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002011 dev = get_cpu_device(cpu);
2012 if (dev) {
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302013
2014 if (action & CPU_TASKS_FROZEN)
2015 frozen = true;
2016
2017 switch (action & ~CPU_TASKS_FROZEN) {
Ashok Rajc32b6b82005-10-30 14:59:54 -08002018 case CPU_ONLINE:
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302019 __cpufreq_add_dev(dev, NULL, frozen);
Srivatsa S. Bhat23d328992013-07-30 04:23:56 +05302020 cpufreq_update_policy(cpu);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002021 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302022
Ashok Rajc32b6b82005-10-30 14:59:54 -08002023 case CPU_DOWN_PREPARE:
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05302024 __cpufreq_remove_dev_prepare(dev, NULL, frozen);
Srivatsa S. Bhat1aee40a2013-09-07 01:23:27 +05302025 break;
2026
2027 case CPU_POST_DEAD:
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05302028 __cpufreq_remove_dev_finish(dev, NULL, frozen);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002029 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302030
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002031 case CPU_DOWN_FAILED:
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302032 __cpufreq_add_dev(dev, NULL, frozen);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002033 break;
2034 }
2035 }
2036 return NOTIFY_OK;
2037}
2038
Neal Buckendahl9c36f742010-06-22 22:02:44 -05002039static struct notifier_block __refdata cpufreq_cpu_notifier = {
Viresh Kumarbb176f72013-06-19 14:19:33 +05302040 .notifier_call = cpufreq_cpu_callback,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002041};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
2043/*********************************************************************
2044 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2045 *********************************************************************/
2046
2047/**
2048 * cpufreq_register_driver - register a CPU Frequency driver
2049 * @driver_data: A struct cpufreq_driver containing the values#
2050 * submitted by the CPU Frequency driver.
2051 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302052 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05002054 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 *
2056 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002057int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
2059 unsigned long flags;
2060 int ret;
2061
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002062 if (cpufreq_disabled())
2063 return -ENODEV;
2064
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 if (!driver_data || !driver_data->verify || !driver_data->init ||
2066 ((!driver_data->setpolicy) && (!driver_data->target)))
2067 return -EINVAL;
2068
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002069 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
2071 if (driver_data->setpolicy)
2072 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2073
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002074 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002075 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002076 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Yinghai Lu4dea58062013-09-18 21:05:20 -07002077 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002079 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002080 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002082 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002083 if (ret)
2084 goto err_null_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002086 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 int i;
2088 ret = -ENODEV;
2089
2090 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07002091 for (i = 0; i < nr_cpu_ids; i++)
2092 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07002094 break;
2095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
2097 /* if all ->init() calls failed, unregister */
2098 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002099 pr_debug("no CPU initialized for driver %s\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05302100 driver_data->name);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002101 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 }
2103 }
2104
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002105 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002106 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002108 return 0;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002109err_if_unreg:
2110 subsys_interface_unregister(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002111err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002112 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002113 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002114 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05002115 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116}
2117EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2118
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119/**
2120 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2121 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302122 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 * the right to do so, i.e. if you have succeeded in initialising before!
2124 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2125 * currently not initialised.
2126 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002127int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128{
2129 unsigned long flags;
2130
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002131 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002134 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002136 subsys_interface_unregister(&cpufreq_interface);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07002137 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
Viresh Kumar6eed9402013-08-06 22:53:11 +05302139 down_write(&cpufreq_rwsem);
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002140 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302141
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002142 cpufreq_driver = NULL;
Viresh Kumar6eed9402013-08-06 22:53:11 +05302143
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002144 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302145 up_write(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
2147 return 0;
2148}
2149EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002150
2151static int __init cpufreq_core_init(void)
2152{
2153 int cpu;
2154
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002155 if (cpufreq_disabled())
2156 return -ENODEV;
2157
Viresh Kumar474deff2013-08-20 12:08:25 +05302158 for_each_possible_cpu(cpu)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002159 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002160
Viresh Kumar2361be22013-05-17 16:09:09 +05302161 cpufreq_global_kobject = kobject_create();
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002162 BUG_ON(!cpufreq_global_kobject);
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01002163 register_syscore_ops(&cpufreq_syscore_ops);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002164
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002165 return 0;
2166}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002167core_initcall(cpufreq_core_init);