blob: 247842b2ee2d7428e9632cfa7e84689c30988c52 [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 Kumarfa1d8af2013-02-07 15:38:42 +053070static int 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 return 0; \
77}
78
79lock_policy_rwsem(read, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080080lock_policy_rwsem(write, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080081
Viresh Kumarfa1d8af2013-02-07 15:38:42 +053082#define unlock_policy_rwsem(mode, cpu) \
83static void unlock_policy_rwsem_##mode(int cpu) \
84{ \
Viresh Kumar474deff2013-08-20 12:08:25 +053085 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); \
86 BUG_ON(!policy); \
87 up_##mode(&per_cpu(cpu_policy_rwsem, policy->cpu)); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080088}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080089
Viresh Kumarfa1d8af2013-02-07 15:38:42 +053090unlock_policy_rwsem(read, cpu);
91unlock_policy_rwsem(write, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080092
Viresh Kumar6eed9402013-08-06 22:53:11 +053093/*
94 * rwsem to guarantee that cpufreq driver module doesn't unload during critical
95 * sections
96 */
97static DECLARE_RWSEM(cpufreq_rwsem);
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/* internal prototypes */
Dave Jones29464f22009-01-18 01:37:11 -0500100static int __cpufreq_governor(struct cpufreq_policy *policy,
101 unsigned int event);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800102static unsigned int __cpufreq_get(unsigned int cpu);
David Howells65f27f32006-11-22 14:55:48 +0000103static void handle_update(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105/**
Dave Jones32ee8c32006-02-28 00:43:23 -0500106 * Two notifier lists: the "policy" list is involved in the
107 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * "transition" list for kernel code that needs to handle
109 * changes to devices when the CPU clock speed changes.
110 * The mutex locks both lists.
111 */
Alan Sterne041c682006-03-27 01:16:30 -0800112static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700113static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200115static bool init_cpufreq_transition_notifier_list_called;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700116static int __init init_cpufreq_transition_notifier_list(void)
117{
118 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200119 init_cpufreq_transition_notifier_list_called = true;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700120 return 0;
121}
Linus Torvaldsb3438f82006-11-20 11:47:18 -0800122pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400124static int off __read_mostly;
Viresh Kumarda584452012-10-26 00:51:32 +0200125static int cpufreq_disabled(void)
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400126{
127 return off;
128}
129void disable_cpufreq(void)
130{
131 off = 1;
132}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133static LIST_HEAD(cpufreq_governor_list);
Dave Jones29464f22009-01-18 01:37:11 -0500134static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000136bool have_governor_per_policy(void)
137{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200138 return cpufreq_driver->have_governor_per_policy;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000139}
Viresh Kumar3f869d62013-05-16 05:09:56 +0000140EXPORT_SYMBOL_GPL(have_governor_per_policy);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000141
Viresh Kumar944e9a02013-05-16 05:09:57 +0000142struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
143{
144 if (have_governor_per_policy())
145 return &policy->kobj;
146 else
147 return cpufreq_global_kobject;
148}
149EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
150
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000151static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
152{
153 u64 idle_time;
154 u64 cur_wall_time;
155 u64 busy_time;
156
157 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
158
159 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
160 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
161 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
162 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
163 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
164 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
165
166 idle_time = cur_wall_time - busy_time;
167 if (wall)
168 *wall = cputime_to_usecs(cur_wall_time);
169
170 return cputime_to_usecs(idle_time);
171}
172
173u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
174{
175 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
176
177 if (idle_time == -1ULL)
178 return get_cpu_idle_time_jiffy(cpu, wall);
179 else if (!io_busy)
180 idle_time += get_cpu_iowait_time_us(cpu, wall);
181
182 return idle_time;
183}
184EXPORT_SYMBOL_GPL(get_cpu_idle_time);
185
Viresh Kumar6eed9402013-08-06 22:53:11 +0530186struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530188 struct cpufreq_policy *policy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 unsigned long flags;
190
Viresh Kumar6eed9402013-08-06 22:53:11 +0530191 if (cpufreq_disabled() || (cpu >= nr_cpu_ids))
192 return NULL;
193
194 if (!down_read_trylock(&cpufreq_rwsem))
195 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 /* get the cpufreq driver */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000198 read_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Viresh Kumar6eed9402013-08-06 22:53:11 +0530200 if (cpufreq_driver) {
201 /* get the CPU */
202 policy = per_cpu(cpufreq_cpu_data, cpu);
203 if (policy)
204 kobject_get(&policy->kobj);
205 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200206
Viresh Kumar6eed9402013-08-06 22:53:11 +0530207 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530209 if (!policy)
Viresh Kumar6eed9402013-08-06 22:53:11 +0530210 up_read(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530212 return policy;
Stephen Boyda9144432012-07-20 18:14:38 +0000213}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
215
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530216void cpufreq_cpu_put(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000218 if (cpufreq_disabled())
219 return;
220
Viresh Kumar6eed9402013-08-06 22:53:11 +0530221 kobject_put(&policy->kobj);
222 up_read(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
228 *********************************************************************/
229
230/**
231 * adjust_jiffies - adjust the system "loops_per_jiffy"
232 *
233 * This function alters the system "loops_per_jiffy" for the clock
234 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500235 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 * per-CPU loops_per_jiffy value wherever possible.
237 */
238#ifndef CONFIG_SMP
239static unsigned long l_p_j_ref;
Viresh Kumarbb176f72013-06-19 14:19:33 +0530240static unsigned int l_p_j_ref_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Arjan van de Ven858119e2006-01-14 13:20:43 -0800242static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 if (ci->flags & CPUFREQ_CONST_LOOPS)
245 return;
246
247 if (!l_p_j_ref_freq) {
248 l_p_j_ref = loops_per_jiffy;
249 l_p_j_ref_freq = ci->old;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200250 pr_debug("saving %lu as reference value for loops_per_jiffy; "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530251 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
Viresh Kumarbb176f72013-06-19 14:19:33 +0530253 if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700254 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530255 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
256 ci->new);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200257 pr_debug("scaling loops_per_jiffy to %lu "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530258 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260}
261#else
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530262static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
263{
264 return;
265}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266#endif
267
Viresh Kumar0956df9c2013-06-19 14:19:34 +0530268static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530269 struct cpufreq_freqs *freqs, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 BUG_ON(irqs_disabled());
272
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000273 if (cpufreq_disabled())
274 return;
275
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200276 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200277 pr_debug("notification %u of frequency transition to %u kHz\n",
Dave Jonese4472cb2006-01-31 15:53:55 -0800278 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500283 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800284 * which is not equal to what the cpufreq core thinks is
285 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200287 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800288 if ((policy) && (policy->cpu == freqs->cpu) &&
289 (policy->cur) && (policy->cur != freqs->old)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200290 pr_debug("Warning: CPU frequency is"
Dave Jonese4472cb2006-01-31 15:53:55 -0800291 " %u, cpufreq assumed %u kHz.\n",
292 freqs->old, policy->cur);
293 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
295 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700296 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800297 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
299 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 case CPUFREQ_POSTCHANGE:
302 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200303 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
Thomas Renninger6f4f2722010-04-20 13:17:36 +0200304 (unsigned long)freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100305 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700306 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800307 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800308 if (likely(policy) && likely(policy->cpu == freqs->cpu))
309 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 break;
311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
Viresh Kumarbb176f72013-06-19 14:19:33 +0530313
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530314/**
315 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
316 * on frequency transition.
317 *
318 * This function calls the transition notifiers and the "adjust_jiffies"
319 * function. It is called twice on all CPU frequency changes that have
320 * external effects.
321 */
322void cpufreq_notify_transition(struct cpufreq_policy *policy,
323 struct cpufreq_freqs *freqs, unsigned int state)
324{
325 for_each_cpu(freqs->cpu, policy->cpus)
326 __cpufreq_notify_transition(policy, freqs, state);
327}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
329
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331/*********************************************************************
332 * SYSFS INTERFACE *
333 *********************************************************************/
334
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700335static struct cpufreq_governor *__find_governor(const char *str_governor)
336{
337 struct cpufreq_governor *t;
338
339 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
Dave Jones29464f22009-01-18 01:37:11 -0500340 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700341 return t;
342
343 return NULL;
344}
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346/**
347 * cpufreq_parse_governor - parse a governor string
348 */
Dave Jones905d77c2008-03-05 14:28:32 -0500349static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 struct cpufreq_governor **governor)
351{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700352 int err = -EINVAL;
353
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200354 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700355 goto out;
356
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200357 if (cpufreq_driver->setpolicy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
359 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700360 err = 0;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530361 } else if (!strnicmp(str_governor, "powersave",
362 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700364 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200366 } else if (cpufreq_driver->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700368
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800369 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700370
371 t = __find_governor(str_governor);
372
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700373 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700374 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700375
Kees Cook1a8e1462011-05-04 08:38:56 -0700376 mutex_unlock(&cpufreq_governor_mutex);
377 ret = request_module("cpufreq_%s", str_governor);
378 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700379
Kees Cook1a8e1462011-05-04 08:38:56 -0700380 if (ret == 0)
381 t = __find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700382 }
383
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700384 if (t != NULL) {
385 *governor = t;
386 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700388
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800389 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
Dave Jones29464f22009-01-18 01:37:11 -0500391out:
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700392 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530396 * cpufreq_per_cpu_attr_read() / show_##file_name() -
397 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 *
399 * Write out information from cpufreq_driver->policy[cpu]; object must be
400 * "unsigned int".
401 */
402
Dave Jones32ee8c32006-02-28 00:43:23 -0500403#define show_one(file_name, object) \
404static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500405(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500406{ \
Dave Jones29464f22009-01-18 01:37:11 -0500407 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
410show_one(cpuinfo_min_freq, cpuinfo.min_freq);
411show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100412show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413show_one(scaling_min_freq, min);
414show_one(scaling_max_freq, max);
415show_one(scaling_cur_freq, cur);
416
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530417static int __cpufreq_set_policy(struct cpufreq_policy *policy,
418 struct cpufreq_policy *new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420/**
421 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
422 */
423#define store_one(file_name, object) \
424static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500425(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{ \
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530427 int ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 struct cpufreq_policy new_policy; \
429 \
430 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
431 if (ret) \
432 return -EINVAL; \
433 \
Dave Jones29464f22009-01-18 01:37:11 -0500434 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (ret != 1) \
436 return -EINVAL; \
437 \
Thomas Renninger7970e082006-04-13 15:14:04 +0200438 ret = __cpufreq_set_policy(policy, &new_policy); \
439 policy->user_policy.object = policy->object; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 \
441 return ret ? ret : count; \
442}
443
Dave Jones29464f22009-01-18 01:37:11 -0500444store_one(scaling_min_freq, min);
445store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447/**
448 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
449 */
Dave Jones905d77c2008-03-05 14:28:32 -0500450static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
451 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800453 unsigned int cur_freq = __cpufreq_get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (!cur_freq)
455 return sprintf(buf, "<unknown>");
456 return sprintf(buf, "%u\n", cur_freq);
457}
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459/**
460 * show_scaling_governor - show the current policy for the specified CPU
461 */
Dave Jones905d77c2008-03-05 14:28:32 -0500462static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Dave Jones29464f22009-01-18 01:37:11 -0500464 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return sprintf(buf, "powersave\n");
466 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
467 return sprintf(buf, "performance\n");
468 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200469 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500470 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return -EINVAL;
472}
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474/**
475 * store_scaling_governor - store policy for the specified CPU
476 */
Dave Jones905d77c2008-03-05 14:28:32 -0500477static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
478 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530480 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 char str_governor[16];
482 struct cpufreq_policy new_policy;
483
484 ret = cpufreq_get_policy(&new_policy, policy->cpu);
485 if (ret)
486 return ret;
487
Dave Jones29464f22009-01-18 01:37:11 -0500488 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (ret != 1)
490 return -EINVAL;
491
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530492 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
493 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return -EINVAL;
495
Viresh Kumarbb176f72013-06-19 14:19:33 +0530496 /*
497 * Do not use cpufreq_set_policy here or the user_policy.max
498 * will be wrongly overridden
499 */
Thomas Renninger7970e082006-04-13 15:14:04 +0200500 ret = __cpufreq_set_policy(policy, &new_policy);
501
502 policy->user_policy.policy = policy->policy;
503 policy->user_policy.governor = policy->governor;
Thomas Renninger7970e082006-04-13 15:14:04 +0200504
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530505 if (ret)
506 return ret;
507 else
508 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
511/**
512 * show_scaling_driver - show the cpufreq driver currently loaded
513 */
Dave Jones905d77c2008-03-05 14:28:32 -0500514static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200516 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
519/**
520 * show_scaling_available_governors - show the available CPUfreq governors
521 */
Dave Jones905d77c2008-03-05 14:28:32 -0500522static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
523 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
525 ssize_t i = 0;
526 struct cpufreq_governor *t;
527
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200528 if (!cpufreq_driver->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 i += sprintf(buf, "performance powersave");
530 goto out;
531 }
532
533 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
Dave Jones29464f22009-01-18 01:37:11 -0500534 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
535 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200537 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500539out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 i += sprintf(&buf[i], "\n");
541 return i;
542}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700543
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800544ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 ssize_t i = 0;
547 unsigned int cpu;
548
Rusty Russell835481d2009-01-04 05:18:06 -0800549 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (i)
551 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
552 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
553 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500554 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556 i += sprintf(&buf[i], "\n");
557 return i;
558}
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800559EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700561/**
562 * show_related_cpus - show the CPUs affected by each transition even if
563 * hw coordination is in use
564 */
565static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
566{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800567 return cpufreq_show_cpus(policy->related_cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700568}
569
570/**
571 * show_affected_cpus - show the CPUs affected by each transition
572 */
573static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
574{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800575 return cpufreq_show_cpus(policy->cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700576}
577
Venki Pallipadi9e769882007-10-26 10:18:21 -0700578static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500579 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700580{
581 unsigned int freq = 0;
582 unsigned int ret;
583
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700584 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700585 return -EINVAL;
586
587 ret = sscanf(buf, "%u", &freq);
588 if (ret != 1)
589 return -EINVAL;
590
591 policy->governor->store_setspeed(policy, freq);
592
593 return count;
594}
595
596static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
597{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700598 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700599 return sprintf(buf, "<unsupported>\n");
600
601 return policy->governor->show_setspeed(policy, buf);
602}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Thomas Renningere2f74f32009-11-19 12:31:01 +0100604/**
viresh kumar8bf1ac722012-10-23 01:23:33 +0200605 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100606 */
607static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
608{
609 unsigned int limit;
610 int ret;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200611 if (cpufreq_driver->bios_limit) {
612 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
Thomas Renningere2f74f32009-11-19 12:31:01 +0100613 if (!ret)
614 return sprintf(buf, "%u\n", limit);
615 }
616 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
617}
618
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200619cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
620cpufreq_freq_attr_ro(cpuinfo_min_freq);
621cpufreq_freq_attr_ro(cpuinfo_max_freq);
622cpufreq_freq_attr_ro(cpuinfo_transition_latency);
623cpufreq_freq_attr_ro(scaling_available_governors);
624cpufreq_freq_attr_ro(scaling_driver);
625cpufreq_freq_attr_ro(scaling_cur_freq);
626cpufreq_freq_attr_ro(bios_limit);
627cpufreq_freq_attr_ro(related_cpus);
628cpufreq_freq_attr_ro(affected_cpus);
629cpufreq_freq_attr_rw(scaling_min_freq);
630cpufreq_freq_attr_rw(scaling_max_freq);
631cpufreq_freq_attr_rw(scaling_governor);
632cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Dave Jones905d77c2008-03-05 14:28:32 -0500634static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 &cpuinfo_min_freq.attr,
636 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100637 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 &scaling_min_freq.attr,
639 &scaling_max_freq.attr,
640 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700641 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 &scaling_governor.attr,
643 &scaling_driver.attr,
644 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700645 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 NULL
647};
648
Dave Jones29464f22009-01-18 01:37:11 -0500649#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
650#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Dave Jones29464f22009-01-18 01:37:11 -0500652static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Dave Jones905d77c2008-03-05 14:28:32 -0500654 struct cpufreq_policy *policy = to_policy(kobj);
655 struct freq_attr *fattr = to_attr(attr);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500656 ssize_t ret = -EINVAL;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530657
658 if (!down_read_trylock(&cpufreq_rwsem))
659 goto exit;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800660
661 if (lock_policy_rwsem_read(policy->cpu) < 0)
Viresh Kumar6eed9402013-08-06 22:53:11 +0530662 goto up_read;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800663
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530664 if (fattr->show)
665 ret = fattr->show(policy, buf);
666 else
667 ret = -EIO;
668
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800669 unlock_policy_rwsem_read(policy->cpu);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530670
671up_read:
672 up_read(&cpufreq_rwsem);
673exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return ret;
675}
676
Dave Jones905d77c2008-03-05 14:28:32 -0500677static ssize_t store(struct kobject *kobj, struct attribute *attr,
678 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Dave Jones905d77c2008-03-05 14:28:32 -0500680 struct cpufreq_policy *policy = to_policy(kobj);
681 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500682 ssize_t ret = -EINVAL;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530683
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530684 get_online_cpus();
685
686 if (!cpu_online(policy->cpu))
687 goto unlock;
688
Viresh Kumar6eed9402013-08-06 22:53:11 +0530689 if (!down_read_trylock(&cpufreq_rwsem))
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530690 goto unlock;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800691
692 if (lock_policy_rwsem_write(policy->cpu) < 0)
Viresh Kumar6eed9402013-08-06 22:53:11 +0530693 goto up_read;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800694
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530695 if (fattr->store)
696 ret = fattr->store(policy, buf, count);
697 else
698 ret = -EIO;
699
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800700 unlock_policy_rwsem_write(policy->cpu);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530701
702up_read:
703 up_read(&cpufreq_rwsem);
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530704unlock:
705 put_online_cpus();
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return ret;
708}
709
Dave Jones905d77c2008-03-05 14:28:32 -0500710static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Dave Jones905d77c2008-03-05 14:28:32 -0500712 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200713 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 complete(&policy->kobj_unregister);
715}
716
Emese Revfy52cf25d2010-01-19 02:58:23 +0100717static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 .show = show,
719 .store = store,
720};
721
722static struct kobj_type ktype_cpufreq = {
723 .sysfs_ops = &sysfs_ops,
724 .default_attrs = default_attrs,
725 .release = cpufreq_sysfs_release,
726};
727
Viresh Kumar2361be22013-05-17 16:09:09 +0530728struct kobject *cpufreq_global_kobject;
729EXPORT_SYMBOL(cpufreq_global_kobject);
730
731static int cpufreq_global_kobject_usage;
732
733int cpufreq_get_global_kobject(void)
734{
735 if (!cpufreq_global_kobject_usage++)
736 return kobject_add(cpufreq_global_kobject,
737 &cpu_subsys.dev_root->kobj, "%s", "cpufreq");
738
739 return 0;
740}
741EXPORT_SYMBOL(cpufreq_get_global_kobject);
742
743void cpufreq_put_global_kobject(void)
744{
745 if (!--cpufreq_global_kobject_usage)
746 kobject_del(cpufreq_global_kobject);
747}
748EXPORT_SYMBOL(cpufreq_put_global_kobject);
749
750int cpufreq_sysfs_create_file(const struct attribute *attr)
751{
752 int ret = cpufreq_get_global_kobject();
753
754 if (!ret) {
755 ret = sysfs_create_file(cpufreq_global_kobject, attr);
756 if (ret)
757 cpufreq_put_global_kobject();
758 }
759
760 return ret;
761}
762EXPORT_SYMBOL(cpufreq_sysfs_create_file);
763
764void cpufreq_sysfs_remove_file(const struct attribute *attr)
765{
766 sysfs_remove_file(cpufreq_global_kobject, attr);
767 cpufreq_put_global_kobject();
768}
769EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
770
Dave Jones19d6f7e2009-07-08 17:35:39 -0400771/* symlink affected CPUs */
Viresh Kumar308b60e2013-07-31 14:35:14 +0200772static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400773{
774 unsigned int j;
775 int ret = 0;
776
777 for_each_cpu(j, policy->cpus) {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800778 struct device *cpu_dev;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400779
Viresh Kumar308b60e2013-07-31 14:35:14 +0200780 if (j == policy->cpu)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400781 continue;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400782
Viresh Kumare8fdde12013-07-31 14:31:33 +0200783 pr_debug("Adding link for CPU: %u\n", j);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800784 cpu_dev = get_cpu_device(j);
785 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Dave Jones19d6f7e2009-07-08 17:35:39 -0400786 "cpufreq");
Rafael J. Wysocki71c34612013-08-04 01:19:34 +0200787 if (ret)
788 break;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400789 }
790 return ret;
791}
792
Viresh Kumar308b60e2013-07-31 14:35:14 +0200793static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800794 struct device *dev)
Dave Jones909a6942009-07-08 18:05:42 -0400795{
796 struct freq_attr **drv_attr;
Dave Jones909a6942009-07-08 18:05:42 -0400797 int ret = 0;
Dave Jones909a6942009-07-08 18:05:42 -0400798
799 /* prepare interface data */
800 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800801 &dev->kobj, "cpufreq");
Dave Jones909a6942009-07-08 18:05:42 -0400802 if (ret)
803 return ret;
804
805 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200806 drv_attr = cpufreq_driver->attr;
Dave Jones909a6942009-07-08 18:05:42 -0400807 while ((drv_attr) && (*drv_attr)) {
808 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->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 drv_attr++;
812 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200813 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -0400814 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
815 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200816 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400817 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200818 if (cpufreq_driver->target) {
Dave Jones909a6942009-07-08 18:05:42 -0400819 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
820 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200821 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400822 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200823 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +0100824 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
825 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200826 goto err_out_kobj_put;
Thomas Renningere2f74f32009-11-19 12:31:01 +0100827 }
Dave Jones909a6942009-07-08 18:05:42 -0400828
Viresh Kumar308b60e2013-07-31 14:35:14 +0200829 ret = cpufreq_add_dev_symlink(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400830 if (ret)
831 goto err_out_kobj_put;
832
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530833 return ret;
834
835err_out_kobj_put:
836 kobject_put(&policy->kobj);
837 wait_for_completion(&policy->kobj_unregister);
838 return ret;
839}
840
841static void cpufreq_init_policy(struct cpufreq_policy *policy)
842{
843 struct cpufreq_policy new_policy;
844 int ret = 0;
845
Viresh Kumard5b73cd2013-08-06 22:53:06 +0530846 memcpy(&new_policy, policy, sizeof(*policy));
Dave Jonesecf7e462009-07-08 18:48:47 -0400847 /* assure that the starting sequence is run in __cpufreq_set_policy */
848 policy->governor = NULL;
849
850 /* set default policy */
851 ret = __cpufreq_set_policy(policy, &new_policy);
852 policy->user_policy.policy = policy->policy;
853 policy->user_policy.governor = policy->governor;
854
855 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200856 pr_debug("setting policy failed\n");
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200857 if (cpufreq_driver->exit)
858 cpufreq_driver->exit(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400859 }
Dave Jones909a6942009-07-08 18:05:42 -0400860}
861
Viresh Kumarfcf80582013-01-29 14:39:08 +0000862#ifdef CONFIG_HOTPLUG_CPU
Viresh Kumard8d3b472013-08-04 01:20:07 +0200863static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,
864 unsigned int cpu, struct device *dev,
865 bool frozen)
Viresh Kumarfcf80582013-01-29 14:39:08 +0000866{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200867 int ret = 0, has_target = !!cpufreq_driver->target;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000868 unsigned long flags;
869
Viresh Kumar3de9bde2013-08-06 22:53:13 +0530870 if (has_target) {
871 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
872 if (ret) {
873 pr_err("%s: Failed to stop governor\n", __func__);
874 return ret;
875 }
876 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000877
Viresh Kumard8d3b472013-08-04 01:20:07 +0200878 lock_policy_rwsem_write(policy->cpu);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530879
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000880 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530881
Viresh Kumarfcf80582013-01-29 14:39:08 +0000882 cpumask_set_cpu(cpu, policy->cpus);
883 per_cpu(cpufreq_cpu_data, cpu) = policy;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000884 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000885
Viresh Kumard8d3b472013-08-04 01:20:07 +0200886 unlock_policy_rwsem_write(policy->cpu);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530887
Viresh Kumar820c6ca2013-04-22 00:48:03 +0200888 if (has_target) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +0530889 if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
890 (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
891 pr_err("%s: Failed to start governor\n", __func__);
892 return ret;
893 }
Viresh Kumar820c6ca2013-04-22 00:48:03 +0200894 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000895
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +0530896 /* Don't touch sysfs links during light-weight init */
Rafael J. Wysocki71c34612013-08-04 01:19:34 +0200897 if (!frozen)
898 ret = sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
Viresh Kumarfcf80582013-01-29 14:39:08 +0000899
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +0530900 return ret;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000901}
902#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530904static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu)
905{
906 struct cpufreq_policy *policy;
907 unsigned long flags;
908
909 write_lock_irqsave(&cpufreq_driver_lock, flags);
910
911 policy = per_cpu(cpufreq_cpu_data_fallback, cpu);
912
913 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
914
915 return policy;
916}
917
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +0530918static struct cpufreq_policy *cpufreq_policy_alloc(void)
919{
920 struct cpufreq_policy *policy;
921
922 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
923 if (!policy)
924 return NULL;
925
926 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
927 goto err_free_policy;
928
929 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
930 goto err_free_cpumask;
931
Lukasz Majewskic88a1f82013-08-06 22:53:08 +0530932 INIT_LIST_HEAD(&policy->policy_list);
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +0530933 return policy;
934
935err_free_cpumask:
936 free_cpumask_var(policy->cpus);
937err_free_policy:
938 kfree(policy);
939
940 return NULL;
941}
942
943static void cpufreq_policy_free(struct cpufreq_policy *policy)
944{
945 free_cpumask_var(policy->related_cpus);
946 free_cpumask_var(policy->cpus);
947 kfree(policy);
948}
949
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +0530950static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
951{
952 policy->last_cpu = policy->cpu;
953 policy->cpu = cpu;
954
955#ifdef CONFIG_CPU_FREQ_TABLE
956 cpufreq_frequency_table_update_policy_cpu(policy);
957#endif
958 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
959 CPUFREQ_UPDATE_POLICY_CPU, policy);
960}
961
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +0530962static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif,
963 bool frozen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Viresh Kumarfcf80582013-01-29 14:39:08 +0000965 unsigned int j, cpu = dev->id;
Viresh Kumar65922462013-02-07 10:56:03 +0530966 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 unsigned long flags;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500969#ifdef CONFIG_HOTPLUG_CPU
Viresh Kumar1b274292013-08-20 12:08:26 +0530970 struct cpufreq_policy *tpolicy;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000971 struct cpufreq_governor *gov;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500972#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Ashok Rajc32b6b82005-10-30 14:59:54 -0800974 if (cpu_is_offline(cpu))
975 return 0;
976
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200977 pr_debug("adding CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979#ifdef CONFIG_SMP
980 /* check whether a different CPU already registered this
981 * CPU because it is in the same boat. */
982 policy = cpufreq_cpu_get(cpu);
983 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -0500984 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return 0;
986 }
Li Zhong5025d622013-08-21 01:31:08 +0200987#endif
Viresh Kumarfcf80582013-01-29 14:39:08 +0000988
Viresh Kumar6eed9402013-08-06 22:53:11 +0530989 if (!down_read_trylock(&cpufreq_rwsem))
990 return 0;
991
Viresh Kumarfcf80582013-01-29 14:39:08 +0000992#ifdef CONFIG_HOTPLUG_CPU
993 /* Check if this cpu was hot-unplugged earlier and has siblings */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000994 read_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar1b274292013-08-20 12:08:26 +0530995 list_for_each_entry(tpolicy, &cpufreq_policy_list, policy_list) {
996 if (cpumask_test_cpu(cpu, tpolicy->related_cpus)) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000997 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar1b274292013-08-20 12:08:26 +0530998 ret = cpufreq_add_policy_cpu(tpolicy, cpu, dev, frozen);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530999 up_read(&cpufreq_rwsem);
1000 return ret;
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301001 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001002 }
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001003 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +00001004#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301006 if (frozen)
1007 /* Restore the saved policy when doing light-weight init */
1008 policy = cpufreq_policy_restore(cpu);
1009 else
1010 policy = cpufreq_policy_alloc();
1011
Dave Jones059019a2009-07-08 16:30:03 -04001012 if (!policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 goto nomem_out;
Dave Jones059019a2009-07-08 16:30:03 -04001014
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301015
1016 /*
1017 * In the resume path, since we restore a saved policy, the assignment
1018 * to policy->cpu is like an update of the existing policy, rather than
1019 * the creation of a brand new one. So we need to perform this update
1020 * by invoking update_policy_cpu().
1021 */
1022 if (frozen && cpu != policy->cpu)
1023 update_policy_cpu(policy, cpu);
1024 else
1025 policy->cpu = cpu;
1026
Viresh Kumar65922462013-02-07 10:56:03 +05301027 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Rusty Russell835481d2009-01-04 05:18:06 -08001028 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 init_completion(&policy->kobj_unregister);
David Howells65f27f32006-11-22 14:55:48 +00001031 INIT_WORK(&policy->update, handle_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 /* call driver. From then on the cpufreq must be able
1034 * to accept all calls to ->verify and ->setpolicy for this CPU
1035 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001036 ret = cpufreq_driver->init(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001038 pr_debug("initialization failed\n");
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301039 goto err_set_policy_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001041
Viresh Kumarfcf80582013-01-29 14:39:08 +00001042 /* related cpus should atleast have policy->cpus */
1043 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
1044
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001045 /*
1046 * affected cpus must always be the one, which are online. We aren't
1047 * managing offline cpus here.
1048 */
1049 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1050
Mike Chan187d9f42008-12-04 12:19:17 -08001051 policy->user_policy.min = policy->min;
1052 policy->user_policy.max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Thomas Renningera1531ac2008-07-29 22:32:58 -07001054 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1055 CPUFREQ_START, policy);
1056
Viresh Kumarfcf80582013-01-29 14:39:08 +00001057#ifdef CONFIG_HOTPLUG_CPU
1058 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
1059 if (gov) {
1060 policy->governor = gov;
1061 pr_debug("Restoring governor %s for cpu %d\n",
1062 policy->governor->name, cpu);
Thomas Renninger4bfa0422009-07-24 15:25:03 +02001063 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001064#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301066 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar474deff2013-08-20 12:08:25 +05301067 for_each_cpu(j, policy->cpus)
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301068 per_cpu(cpufreq_cpu_data, j) = policy;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301069 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1070
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301071 if (!frozen) {
Viresh Kumar308b60e2013-07-31 14:35:14 +02001072 ret = cpufreq_add_dev_interface(policy, dev);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301073 if (ret)
1074 goto err_out_unregister;
1075 }
Dave Jones8ff69732006-03-05 03:37:23 -05001076
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301077 write_lock_irqsave(&cpufreq_driver_lock, flags);
1078 list_add(&policy->policy_list, &cpufreq_policy_list);
1079 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1080
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301081 cpufreq_init_policy(policy);
1082
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001083 kobject_uevent(&policy->kobj, KOBJ_ADD);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301084 up_read(&cpufreq_rwsem);
1085
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001086 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return 0;
1089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090err_out_unregister:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001091 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar474deff2013-08-20 12:08:25 +05301092 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -07001093 per_cpu(cpufreq_cpu_data, j) = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001094 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301096err_set_policy_cpu:
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301097 cpufreq_policy_free(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098nomem_out:
Viresh Kumar6eed9402013-08-06 22:53:11 +05301099 up_read(&cpufreq_rwsem);
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 return ret;
1102}
1103
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301104/**
1105 * cpufreq_add_dev - add a CPU device
1106 *
1107 * Adds the cpufreq interface for a CPU device.
1108 *
1109 * The Oracle says: try running cpufreq registration/unregistration concurrently
1110 * with with cpu hotplugging and all hell will break loose. Tried to clean this
1111 * mess up, but more thorough testing is needed. - Mathieu
1112 */
1113static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1114{
1115 return __cpufreq_add_dev(dev, sif, false);
1116}
1117
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301118static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy *policy,
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301119 unsigned int old_cpu, bool frozen)
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301120{
1121 struct device *cpu_dev;
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301122 int ret;
1123
1124 /* first sibling now owns the new sysfs dir */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301125 cpu_dev = get_cpu_device(cpumask_first(policy->cpus));
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301126
1127 /* Don't touch sysfs files during light-weight tear-down */
1128 if (frozen)
1129 return cpu_dev->id;
1130
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301131 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301132 ret = kobject_move(&policy->kobj, &cpu_dev->kobj);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301133 if (ret) {
1134 pr_err("%s: Failed to move kobj: %d", __func__, ret);
1135
1136 WARN_ON(lock_policy_rwsem_write(old_cpu));
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301137 cpumask_set_cpu(old_cpu, policy->cpus);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301138 unlock_policy_rwsem_write(old_cpu);
1139
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301140 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301141 "cpufreq");
1142
1143 return -EINVAL;
1144 }
1145
1146 return cpu_dev->id;
1147}
1148
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301149static int __cpufreq_remove_dev_prepare(struct device *dev,
1150 struct subsys_interface *sif,
1151 bool frozen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301153 unsigned int cpu = dev->id, cpus;
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301154 int new_cpu, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 unsigned long flags;
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301156 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001158 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001160 write_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301162 policy = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301164 /* Save the policy somewhere when doing a light-weight tear-down */
1165 if (frozen)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301166 per_cpu(cpufreq_cpu_data_fallback, cpu) = policy;
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301167
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001168 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301170 if (!policy) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001171 pr_debug("%s: No cpu_data found\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301175 if (cpufreq_driver->target) {
1176 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1177 if (ret) {
1178 pr_err("%s: Failed to stop governor\n", __func__);
1179 return ret;
1180 }
1181 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001182
Jacob Shin27ecddc2011-04-27 13:32:11 -05001183#ifdef CONFIG_HOTPLUG_CPU
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001184 if (!cpufreq_driver->setpolicy)
Dirk Brandewiefa69e332013-02-06 09:02:11 -08001185 strncpy(per_cpu(cpufreq_cpu_governor, cpu),
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301186 policy->governor->name, CPUFREQ_NAME_LEN);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001187#endif
1188
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301189 WARN_ON(lock_policy_rwsem_write(cpu));
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301190 cpus = cpumask_weight(policy->cpus);
Viresh Kumare4969eb2013-04-11 08:04:53 +00001191
1192 if (cpus > 1)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301193 cpumask_clear_cpu(cpu, policy->cpus);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301194 unlock_policy_rwsem_write(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 Kumar2eaa3e22013-02-07 10:55:00 +05301200
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301201 new_cpu = cpufreq_nominate_new_policy_cpu(policy, cpu, frozen);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301202 if (new_cpu >= 0) {
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301203 WARN_ON(lock_policy_rwsem_write(cpu));
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301204 update_policy_cpu(policy, new_cpu);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301205 unlock_policy_rwsem_write(cpu);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301206
1207 if (!frozen) {
1208 pr_debug("%s: policy Kobject moved to cpu: %d "
1209 "from: %d\n",__func__, new_cpu, cpu);
1210 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001211 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001212 }
Venki Pallipadiec282972007-03-26 12:03:19 -07001213
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301214 return 0;
1215}
1216
1217static int __cpufreq_remove_dev_finish(struct device *dev,
1218 struct subsys_interface *sif,
1219 bool frozen)
1220{
1221 unsigned int cpu = dev->id, cpus;
1222 int ret;
1223 unsigned long flags;
1224 struct cpufreq_policy *policy;
1225 struct kobject *kobj;
1226 struct completion *cmp;
1227
1228 read_lock_irqsave(&cpufreq_driver_lock, flags);
1229 policy = per_cpu(cpufreq_cpu_data, cpu);
1230 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1231
1232 if (!policy) {
1233 pr_debug("%s: No cpu_data found\n", __func__);
1234 return -EINVAL;
1235 }
1236
1237 lock_policy_rwsem_read(cpu);
1238 cpus = cpumask_weight(policy->cpus);
1239 unlock_policy_rwsem_read(cpu);
1240
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/**
1301 * __cpufreq_remove_dev - remove a CPU device
1302 *
1303 * Removes the cpufreq interface for a CPU device.
1304 * Caller should already have policy_rwsem in write mode for this CPU.
1305 * This routine frees the rwsem before returning.
1306 */
1307static inline int __cpufreq_remove_dev(struct device *dev,
1308 struct subsys_interface *sif,
1309 bool frozen)
1310{
1311 int ret;
1312
1313 ret = __cpufreq_remove_dev_prepare(dev, sif, frozen);
1314
1315 if (!ret)
1316 ret = __cpufreq_remove_dev_finish(dev, sif, frozen);
1317
1318 return ret;
1319}
1320
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001321static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001322{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001323 unsigned int cpu = dev->id;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001324 int retval;
Venki Pallipadiec282972007-03-26 12:03:19 -07001325
1326 if (cpu_is_offline(cpu))
1327 return 0;
1328
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301329 retval = __cpufreq_remove_dev(dev, sif, false);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001330 return retval;
1331}
1332
David Howells65f27f32006-11-22 14:55:48 +00001333static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
David Howells65f27f32006-11-22 14:55:48 +00001335 struct cpufreq_policy *policy =
1336 container_of(work, struct cpufreq_policy, update);
1337 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001338 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 cpufreq_update_policy(cpu);
1340}
1341
1342/**
Viresh Kumarbb176f72013-06-19 14:19:33 +05301343 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1344 * in deep trouble.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 * @cpu: cpu number
1346 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1347 * @new_freq: CPU frequency the CPU actually runs at
1348 *
Dave Jones29464f22009-01-18 01:37:11 -05001349 * We adjust to current frequency first, and need to clean up later.
1350 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301352static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1353 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301355 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301357 unsigned long flags;
1358
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001359 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 freqs.old = old_freq;
1363 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301364
1365 read_lock_irqsave(&cpufreq_driver_lock, flags);
1366 policy = per_cpu(cpufreq_cpu_data, cpu);
1367 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1368
1369 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1370 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371}
1372
Dave Jones32ee8c32006-02-28 00:43:23 -05001373/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301374 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001375 * @cpu: CPU number
1376 *
1377 * This is the last known freq, without actually getting it from the driver.
1378 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1379 */
1380unsigned int cpufreq_quick_get(unsigned int cpu)
1381{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001382 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301383 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001384
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001385 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1386 return cpufreq_driver->get(cpu);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001387
1388 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001389 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301390 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001391 cpufreq_cpu_put(policy);
1392 }
1393
Dave Jones4d34a672008-02-07 16:33:49 -05001394 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001395}
1396EXPORT_SYMBOL(cpufreq_quick_get);
1397
Jesse Barnes3d737102011-06-28 10:59:12 -07001398/**
1399 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1400 * @cpu: CPU number
1401 *
1402 * Just return the max possible frequency for a given CPU.
1403 */
1404unsigned int cpufreq_quick_get_max(unsigned int cpu)
1405{
1406 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1407 unsigned int ret_freq = 0;
1408
1409 if (policy) {
1410 ret_freq = policy->max;
1411 cpufreq_cpu_put(policy);
1412 }
1413
1414 return ret_freq;
1415}
1416EXPORT_SYMBOL(cpufreq_quick_get_max);
1417
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001418static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001420 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301421 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001423 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001424 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001426 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301428 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001429 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301430 /* verify no discrepancy between actual and
1431 saved value exists */
1432 if (unlikely(ret_freq != policy->cur)) {
1433 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 schedule_work(&policy->update);
1435 }
1436 }
1437
Dave Jones4d34a672008-02-07 16:33:49 -05001438 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001439}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001441/**
1442 * cpufreq_get - get the current CPU frequency (in kHz)
1443 * @cpu: CPU number
1444 *
1445 * Get the CPU current (static) CPU frequency
1446 */
1447unsigned int cpufreq_get(unsigned int cpu)
1448{
1449 unsigned int ret_freq = 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001450
Viresh Kumar6eed9402013-08-06 22:53:11 +05301451 if (!down_read_trylock(&cpufreq_rwsem))
1452 return 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001453
1454 if (unlikely(lock_policy_rwsem_read(cpu)))
1455 goto out_policy;
1456
1457 ret_freq = __cpufreq_get(cpu);
1458
1459 unlock_policy_rwsem_read(cpu);
1460
1461out_policy:
Viresh Kumar6eed9402013-08-06 22:53:11 +05301462 up_read(&cpufreq_rwsem);
1463
Dave Jones4d34a672008-02-07 16:33:49 -05001464 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465}
1466EXPORT_SYMBOL(cpufreq_get);
1467
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001468static struct subsys_interface cpufreq_interface = {
1469 .name = "cpufreq",
1470 .subsys = &cpu_subsys,
1471 .add_dev = cpufreq_add_dev,
1472 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001473};
1474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001476 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1477 *
1478 * This function is only executed for the boot processor. The other CPUs
1479 * have been put offline by means of CPU hotplug.
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001480 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001481static int cpufreq_bp_suspend(void)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001482{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301483 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001484
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001485 int cpu = smp_processor_id();
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301486 struct cpufreq_policy *policy;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001487
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001488 pr_debug("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001489
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001490 /* If there's no policy for the boot CPU, we have nothing to do. */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301491 policy = cpufreq_cpu_get(cpu);
1492 if (!policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001493 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001494
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001495 if (cpufreq_driver->suspend) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301496 ret = cpufreq_driver->suspend(policy);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001497 if (ret)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001498 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301499 "step on CPU %u\n", policy->cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001500 }
1501
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301502 cpufreq_cpu_put(policy);
Dave Jonesc9060492008-02-07 16:32:18 -05001503 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001504}
1505
1506/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001507 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 *
1509 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001510 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1511 * restored. It will verify that the current freq is in sync with
1512 * what we believe it to be. This is a bit later than when it
1513 * should be, but nonethteless it's better than calling
1514 * cpufreq_driver->get() here which might re-enable interrupts...
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001515 *
1516 * This function is only executed for the boot CPU. The other CPUs have not
1517 * been turned on yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001519static void cpufreq_bp_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301521 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001522
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001523 int cpu = smp_processor_id();
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301524 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001526 pr_debug("resuming cpu %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001528 /* If there's no policy for the boot CPU, we have nothing to do. */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301529 policy = cpufreq_cpu_get(cpu);
1530 if (!policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001531 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001533 if (cpufreq_driver->resume) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301534 ret = cpufreq_driver->resume(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 if (ret) {
1536 printk(KERN_ERR "cpufreq: resume failed in ->resume "
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301537 "step on CPU %u\n", policy->cpu);
Dave Jonesc9060492008-02-07 16:32:18 -05001538 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 }
1540 }
1541
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301542 schedule_work(&policy->update);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001543
Dave Jonesc9060492008-02-07 16:32:18 -05001544fail:
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301545 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546}
1547
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001548static struct syscore_ops cpufreq_syscore_ops = {
1549 .suspend = cpufreq_bp_suspend,
1550 .resume = cpufreq_bp_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551};
1552
Borislav Petkov9d950462013-01-20 10:24:28 +00001553/**
1554 * cpufreq_get_current_driver - return current driver's name
1555 *
1556 * Return the name string of the currently loaded cpufreq driver
1557 * or NULL, if none.
1558 */
1559const char *cpufreq_get_current_driver(void)
1560{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001561 if (cpufreq_driver)
1562 return cpufreq_driver->name;
1563
1564 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001565}
1566EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568/*********************************************************************
1569 * NOTIFIER LISTS INTERFACE *
1570 *********************************************************************/
1571
1572/**
1573 * cpufreq_register_notifier - register a driver with cpufreq
1574 * @nb: notifier function to register
1575 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1576 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001577 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 * are notified about clock rate changes (once before and once after
1579 * the transition), or a list of drivers that are notified about
1580 * changes in cpufreq policy.
1581 *
1582 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001583 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 */
1585int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1586{
1587 int ret;
1588
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001589 if (cpufreq_disabled())
1590 return -EINVAL;
1591
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001592 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 switch (list) {
1595 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001596 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001597 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 break;
1599 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001600 ret = blocking_notifier_chain_register(
1601 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 break;
1603 default:
1604 ret = -EINVAL;
1605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 return ret;
1608}
1609EXPORT_SYMBOL(cpufreq_register_notifier);
1610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611/**
1612 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1613 * @nb: notifier block to be unregistered
Viresh Kumarbb176f72013-06-19 14:19:33 +05301614 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 *
1616 * Remove a driver from the CPU frequency notifier list.
1617 *
1618 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001619 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 */
1621int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1622{
1623 int ret;
1624
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001625 if (cpufreq_disabled())
1626 return -EINVAL;
1627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 switch (list) {
1629 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001630 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001631 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 break;
1633 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001634 ret = blocking_notifier_chain_unregister(
1635 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 break;
1637 default:
1638 ret = -EINVAL;
1639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
1641 return ret;
1642}
1643EXPORT_SYMBOL(cpufreq_unregister_notifier);
1644
1645
1646/*********************************************************************
1647 * GOVERNORS *
1648 *********************************************************************/
1649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650int __cpufreq_driver_target(struct cpufreq_policy *policy,
1651 unsigned int target_freq,
1652 unsigned int relation)
1653{
1654 int retval = -EINVAL;
Viresh Kumar72499242012-10-31 01:28:21 +01001655 unsigned int old_target_freq = target_freq;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001656
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001657 if (cpufreq_disabled())
1658 return -ENODEV;
1659
Viresh Kumar72499242012-10-31 01:28:21 +01001660 /* Make sure that target_freq is within supported range */
1661 if (target_freq > policy->max)
1662 target_freq = policy->max;
1663 if (target_freq < policy->min)
1664 target_freq = policy->min;
1665
1666 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1667 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001668
1669 if (target_freq == policy->cur)
1670 return 0;
1671
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001672 if (cpufreq_driver->target)
1673 retval = cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 return retval;
1676}
1677EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679int cpufreq_driver_target(struct cpufreq_policy *policy,
1680 unsigned int target_freq,
1681 unsigned int relation)
1682{
Julia Lawallf1829e42008-07-25 22:44:53 +02001683 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001685 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
Julia Lawallf1829e42008-07-25 22:44:53 +02001686 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
1688 ret = __cpufreq_driver_target(policy, target_freq, relation);
1689
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001690 unlock_policy_rwsem_write(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Julia Lawallf1829e42008-07-25 22:44:53 +02001692fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 return ret;
1694}
1695EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1696
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001697/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001698 * when "event" is CPUFREQ_GOV_LIMITS
1699 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301701static int __cpufreq_governor(struct cpufreq_policy *policy,
1702 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
Dave Jonescc993ca2005-07-28 09:43:56 -07001704 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001705
1706 /* Only must be defined when default governor is known to have latency
1707 restrictions, like e.g. conservative or ondemand.
1708 That this is the case is already ensured in Kconfig
1709 */
1710#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1711 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1712#else
1713 struct cpufreq_governor *gov = NULL;
1714#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001715
1716 if (policy->governor->max_transition_latency &&
1717 policy->cpuinfo.transition_latency >
1718 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07001719 if (!gov)
1720 return -EINVAL;
1721 else {
1722 printk(KERN_WARNING "%s governor failed, too long"
1723 " transition latency of HW, fallback"
1724 " to %s governor\n",
1725 policy->governor->name,
1726 gov->name);
1727 policy->governor = gov;
1728 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Viresh Kumarfe492f32013-08-06 22:53:10 +05301731 if (event == CPUFREQ_GOV_POLICY_INIT)
1732 if (!try_module_get(policy->governor->owner))
1733 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001735 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301736 policy->cpu, event);
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001737
1738 mutex_lock(&cpufreq_governor_lock);
Srivatsa S. Bhat56d07db2013-09-07 01:23:55 +05301739 if ((policy->governor_enabled && event == CPUFREQ_GOV_START)
Viresh Kumarf73d3932013-08-31 17:53:40 +05301740 || (!policy->governor_enabled
1741 && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) {
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001742 mutex_unlock(&cpufreq_governor_lock);
1743 return -EBUSY;
1744 }
1745
1746 if (event == CPUFREQ_GOV_STOP)
1747 policy->governor_enabled = false;
1748 else if (event == CPUFREQ_GOV_START)
1749 policy->governor_enabled = true;
1750
1751 mutex_unlock(&cpufreq_governor_lock);
1752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 ret = policy->governor->governor(policy, event);
1754
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001755 if (!ret) {
1756 if (event == CPUFREQ_GOV_POLICY_INIT)
1757 policy->governor->initialized++;
1758 else if (event == CPUFREQ_GOV_POLICY_EXIT)
1759 policy->governor->initialized--;
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001760 } else {
1761 /* Restore original values */
1762 mutex_lock(&cpufreq_governor_lock);
1763 if (event == CPUFREQ_GOV_STOP)
1764 policy->governor_enabled = true;
1765 else if (event == CPUFREQ_GOV_START)
1766 policy->governor_enabled = false;
1767 mutex_unlock(&cpufreq_governor_lock);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001768 }
Viresh Kumarb3940582013-02-01 05:42:58 +00001769
Viresh Kumarfe492f32013-08-06 22:53:10 +05301770 if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
1771 ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 module_put(policy->governor->owner);
1773
1774 return ret;
1775}
1776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777int cpufreq_register_governor(struct cpufreq_governor *governor)
1778{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001779 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781 if (!governor)
1782 return -EINVAL;
1783
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001784 if (cpufreq_disabled())
1785 return -ENODEV;
1786
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001787 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001788
Viresh Kumarb3940582013-02-01 05:42:58 +00001789 governor->initialized = 0;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001790 err = -EBUSY;
1791 if (__find_governor(governor->name) == NULL) {
1792 err = 0;
1793 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Dave Jones32ee8c32006-02-28 00:43:23 -05001796 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001797 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798}
1799EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1802{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001803#ifdef CONFIG_HOTPLUG_CPU
1804 int cpu;
1805#endif
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 if (!governor)
1808 return;
1809
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001810 if (cpufreq_disabled())
1811 return;
1812
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001813#ifdef CONFIG_HOTPLUG_CPU
1814 for_each_present_cpu(cpu) {
1815 if (cpu_online(cpu))
1816 continue;
1817 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1818 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1819 }
1820#endif
1821
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001822 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001824 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 return;
1826}
1827EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1828
1829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830/*********************************************************************
1831 * POLICY INTERFACE *
1832 *********************************************************************/
1833
1834/**
1835 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05001836 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1837 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 *
1839 * Reads the current cpufreq policy.
1840 */
1841int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1842{
1843 struct cpufreq_policy *cpu_policy;
1844 if (!policy)
1845 return -EINVAL;
1846
1847 cpu_policy = cpufreq_cpu_get(cpu);
1848 if (!cpu_policy)
1849 return -EINVAL;
1850
Viresh Kumard5b73cd2013-08-06 22:53:06 +05301851 memcpy(policy, cpu_policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
1853 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 return 0;
1855}
1856EXPORT_SYMBOL(cpufreq_get_policy);
1857
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001858/*
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301859 * data : current policy.
1860 * policy : policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001861 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301862static int __cpufreq_set_policy(struct cpufreq_policy *policy,
1863 struct cpufreq_policy *new_policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001865 int ret = 0, failed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301867 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", new_policy->cpu,
1868 new_policy->min, new_policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Viresh Kumard5b73cd2013-08-06 22:53:06 +05301870 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301872 if (new_policy->min > policy->max || new_policy->max < policy->min) {
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02001873 ret = -EINVAL;
1874 goto error_out;
1875 }
1876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 /* verify the cpu speed can be set within this limit */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301878 ret = cpufreq_driver->verify(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 if (ret)
1880 goto error_out;
1881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001883 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301884 CPUFREQ_ADJUST, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
1886 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001887 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301888 CPUFREQ_INCOMPATIBLE, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Viresh Kumarbb176f72013-06-19 14:19:33 +05301890 /*
1891 * verify the cpu speed can be set within this limit, which might be
1892 * different to the first one
1893 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301894 ret = cpufreq_driver->verify(new_policy);
Alan Sterne041c682006-03-27 01:16:30 -08001895 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
1898 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001899 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301900 CPUFREQ_NOTIFY, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301902 policy->min = new_policy->min;
1903 policy->max = new_policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001905 pr_debug("new min and max freqs are %u - %u kHz\n",
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301906 policy->min, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001908 if (cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301909 policy->policy = new_policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001910 pr_debug("setting range\n");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301911 ret = cpufreq_driver->setpolicy(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 } else {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301913 if (new_policy->governor != policy->governor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 /* save old, working values */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301915 struct cpufreq_governor *old_gov = policy->governor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001917 pr_debug("governor switch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
1919 /* end old governor */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301920 if (policy->governor) {
1921 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1922 unlock_policy_rwsem_write(new_policy->cpu);
1923 __cpufreq_governor(policy,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001924 CPUFREQ_GOV_POLICY_EXIT);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301925 lock_policy_rwsem_write(new_policy->cpu);
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
1928 /* start new governor */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301929 policy->governor = new_policy->governor;
1930 if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
1931 if (!__cpufreq_governor(policy, CPUFREQ_GOV_START)) {
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001932 failed = 0;
Viresh Kumar955ef482013-05-16 05:09:58 +00001933 } else {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301934 unlock_policy_rwsem_write(new_policy->cpu);
1935 __cpufreq_governor(policy,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001936 CPUFREQ_GOV_POLICY_EXIT);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301937 lock_policy_rwsem_write(new_policy->cpu);
Viresh Kumar955ef482013-05-16 05:09:58 +00001938 }
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001939 }
1940
1941 if (failed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 /* new governor failed, so re-start old one */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001943 pr_debug("starting governor %s failed\n",
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301944 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 if (old_gov) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301946 policy->governor = old_gov;
1947 __cpufreq_governor(policy,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001948 CPUFREQ_GOV_POLICY_INIT);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301949 __cpufreq_governor(policy,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301950 CPUFREQ_GOV_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 }
1952 ret = -EINVAL;
1953 goto error_out;
1954 }
1955 /* might be a policy change, too, so fall through */
1956 }
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001957 pr_debug("governor: change or update limits\n");
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301958 ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 }
1960
Dave Jones7d5e3502006-02-02 17:03:42 -05001961error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 return ret;
1963}
1964
1965/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1967 * @cpu: CPU which shall be re-evaluated
1968 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001969 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 * at different times.
1971 */
1972int cpufreq_update_policy(unsigned int cpu)
1973{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301974 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1975 struct cpufreq_policy new_policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02001976 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301978 if (!policy) {
Julia Lawallf1829e42008-07-25 22:44:53 +02001979 ret = -ENODEV;
1980 goto no_policy;
1981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
Julia Lawallf1829e42008-07-25 22:44:53 +02001983 if (unlikely(lock_policy_rwsem_write(cpu))) {
1984 ret = -EINVAL;
1985 goto fail;
1986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001988 pr_debug("updating policy for CPU %u\n", cpu);
Viresh Kumard5b73cd2013-08-06 22:53:06 +05301989 memcpy(&new_policy, policy, sizeof(*policy));
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301990 new_policy.min = policy->user_policy.min;
1991 new_policy.max = policy->user_policy.max;
1992 new_policy.policy = policy->user_policy.policy;
1993 new_policy.governor = policy->user_policy.governor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Viresh Kumarbb176f72013-06-19 14:19:33 +05301995 /*
1996 * BIOS might change freq behind our back
1997 * -> ask driver for current freq and notify governors about a change
1998 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001999 if (cpufreq_driver->get) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302000 new_policy.cur = cpufreq_driver->get(cpu);
2001 if (!policy->cur) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002002 pr_debug("Driver did not initialize current freq");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302003 policy->cur = new_policy.cur;
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002004 } else {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302005 if (policy->cur != new_policy.cur && cpufreq_driver->target)
2006 cpufreq_out_of_sync(cpu, policy->cur,
2007 new_policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002008 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01002009 }
2010
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302011 ret = __cpufreq_set_policy(policy, &new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002013 unlock_policy_rwsem_write(cpu);
2014
Julia Lawallf1829e42008-07-25 22:44:53 +02002015fail:
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302016 cpufreq_cpu_put(policy);
Julia Lawallf1829e42008-07-25 22:44:53 +02002017no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 return ret;
2019}
2020EXPORT_SYMBOL(cpufreq_update_policy);
2021
Paul Gortmaker27609842013-06-19 13:54:04 -04002022static int cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002023 unsigned long action, void *hcpu)
2024{
2025 unsigned int cpu = (unsigned long)hcpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002026 struct device *dev;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302027 bool frozen = false;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002028
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002029 dev = get_cpu_device(cpu);
2030 if (dev) {
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302031
2032 if (action & CPU_TASKS_FROZEN)
2033 frozen = true;
2034
2035 switch (action & ~CPU_TASKS_FROZEN) {
Ashok Rajc32b6b82005-10-30 14:59:54 -08002036 case CPU_ONLINE:
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302037 __cpufreq_add_dev(dev, NULL, frozen);
Srivatsa S. Bhat23d328992013-07-30 04:23:56 +05302038 cpufreq_update_policy(cpu);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002039 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302040
Ashok Rajc32b6b82005-10-30 14:59:54 -08002041 case CPU_DOWN_PREPARE:
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05302042 __cpufreq_remove_dev_prepare(dev, NULL, frozen);
Srivatsa S. Bhat1aee40a2013-09-07 01:23:27 +05302043 break;
2044
2045 case CPU_POST_DEAD:
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05302046 __cpufreq_remove_dev_finish(dev, NULL, frozen);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002047 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302048
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002049 case CPU_DOWN_FAILED:
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302050 __cpufreq_add_dev(dev, NULL, frozen);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002051 break;
2052 }
2053 }
2054 return NOTIFY_OK;
2055}
2056
Neal Buckendahl9c36f742010-06-22 22:02:44 -05002057static struct notifier_block __refdata cpufreq_cpu_notifier = {
Viresh Kumarbb176f72013-06-19 14:19:33 +05302058 .notifier_call = cpufreq_cpu_callback,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002059};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061/*********************************************************************
2062 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2063 *********************************************************************/
2064
2065/**
2066 * cpufreq_register_driver - register a CPU Frequency driver
2067 * @driver_data: A struct cpufreq_driver containing the values#
2068 * submitted by the CPU Frequency driver.
2069 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302070 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05002072 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 *
2074 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002075int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076{
2077 unsigned long flags;
2078 int ret;
2079
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002080 if (cpufreq_disabled())
2081 return -ENODEV;
2082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 if (!driver_data || !driver_data->verify || !driver_data->init ||
2084 ((!driver_data->setpolicy) && (!driver_data->target)))
2085 return -EINVAL;
2086
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002087 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
2089 if (driver_data->setpolicy)
2090 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2091
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002092 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002093 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002094 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 return -EBUSY;
2096 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002097 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002098 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002100 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002101 if (ret)
2102 goto err_null_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002104 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 int i;
2106 ret = -ENODEV;
2107
2108 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07002109 for (i = 0; i < nr_cpu_ids; i++)
2110 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07002112 break;
2113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
2115 /* if all ->init() calls failed, unregister */
2116 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002117 pr_debug("no CPU initialized for driver %s\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05302118 driver_data->name);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002119 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 }
2121 }
2122
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002123 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002124 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002126 return 0;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002127err_if_unreg:
2128 subsys_interface_unregister(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002129err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002130 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002131 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002132 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05002133 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134}
2135EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137/**
2138 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2139 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302140 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 * the right to do so, i.e. if you have succeeded in initialising before!
2142 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2143 * currently not initialised.
2144 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002145int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146{
2147 unsigned long flags;
2148
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002149 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002152 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002154 subsys_interface_unregister(&cpufreq_interface);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07002155 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Viresh Kumar6eed9402013-08-06 22:53:11 +05302157 down_write(&cpufreq_rwsem);
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002158 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302159
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002160 cpufreq_driver = NULL;
Viresh Kumar6eed9402013-08-06 22:53:11 +05302161
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002162 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302163 up_write(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
2165 return 0;
2166}
2167EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002168
2169static int __init cpufreq_core_init(void)
2170{
2171 int cpu;
2172
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002173 if (cpufreq_disabled())
2174 return -ENODEV;
2175
Viresh Kumar474deff2013-08-20 12:08:25 +05302176 for_each_possible_cpu(cpu)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002177 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002178
Viresh Kumar2361be22013-05-17 16:09:09 +05302179 cpufreq_global_kobject = kobject_create();
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002180 BUG_ON(!cpufreq_global_kobject);
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01002181 register_syscore_ops(&cpufreq_syscore_ops);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002182
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002183 return 0;
2184}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002185core_initcall(cpufreq_core_init);