blob: d7efdfe0c12cba32f6232dc162b6641d07af3504 [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);
Jane Li6f1e4ef2014-01-03 17:17:41 +080042DEFINE_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
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +053050static inline bool has_target(void)
51{
52 return cpufreq_driver->target_index || cpufreq_driver->target;
53}
54
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080055/*
Viresh Kumar6eed9402013-08-06 22:53:11 +053056 * rwsem to guarantee that cpufreq driver module doesn't unload during critical
57 * sections
58 */
59static DECLARE_RWSEM(cpufreq_rwsem);
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/* internal prototypes */
Dave Jones29464f22009-01-18 01:37:11 -050062static int __cpufreq_governor(struct cpufreq_policy *policy,
63 unsigned int event);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080064static unsigned int __cpufreq_get(unsigned int cpu);
David Howells65f27f32006-11-22 14:55:48 +000065static void handle_update(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67/**
Dave Jones32ee8c32006-02-28 00:43:23 -050068 * Two notifier lists: the "policy" list is involved in the
69 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 * "transition" list for kernel code that needs to handle
71 * changes to devices when the CPU clock speed changes.
72 * The mutex locks both lists.
73 */
Alan Sterne041c682006-03-27 01:16:30 -080074static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -070075static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -020077static bool init_cpufreq_transition_notifier_list_called;
Alan Sternb4dfdbb2006-10-04 02:17:06 -070078static int __init init_cpufreq_transition_notifier_list(void)
79{
80 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -020081 init_cpufreq_transition_notifier_list_called = true;
Alan Sternb4dfdbb2006-10-04 02:17:06 -070082 return 0;
83}
Linus Torvaldsb3438f82006-11-20 11:47:18 -080084pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -040086static int off __read_mostly;
Viresh Kumarda584452012-10-26 00:51:32 +020087static int cpufreq_disabled(void)
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -040088{
89 return off;
90}
91void disable_cpufreq(void)
92{
93 off = 1;
94}
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static LIST_HEAD(cpufreq_governor_list);
Dave Jones29464f22009-01-18 01:37:11 -050096static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000098bool have_governor_per_policy(void)
99{
Viresh Kumar0b981e72013-10-02 14:13:18 +0530100 return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000101}
Viresh Kumar3f869d62013-05-16 05:09:56 +0000102EXPORT_SYMBOL_GPL(have_governor_per_policy);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000103
Viresh Kumar944e9a02013-05-16 05:09:57 +0000104struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
105{
106 if (have_governor_per_policy())
107 return &policy->kobj;
108 else
109 return cpufreq_global_kobject;
110}
111EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
112
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000113static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
114{
115 u64 idle_time;
116 u64 cur_wall_time;
117 u64 busy_time;
118
119 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
120
121 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
122 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
123 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
124 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
125 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
126 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
127
128 idle_time = cur_wall_time - busy_time;
129 if (wall)
130 *wall = cputime_to_usecs(cur_wall_time);
131
132 return cputime_to_usecs(idle_time);
133}
134
135u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
136{
137 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
138
139 if (idle_time == -1ULL)
140 return get_cpu_idle_time_jiffy(cpu, wall);
141 else if (!io_busy)
142 idle_time += get_cpu_iowait_time_us(cpu, wall);
143
144 return idle_time;
145}
146EXPORT_SYMBOL_GPL(get_cpu_idle_time);
147
Viresh Kumar70e9e772013-10-03 20:29:07 +0530148/*
149 * This is a generic cpufreq init() routine which can be used by cpufreq
150 * drivers of SMP systems. It will do following:
151 * - validate & show freq table passed
152 * - set policies transition latency
153 * - policy->cpus with all possible CPUs
154 */
155int cpufreq_generic_init(struct cpufreq_policy *policy,
156 struct cpufreq_frequency_table *table,
157 unsigned int transition_latency)
158{
159 int ret;
160
161 ret = cpufreq_table_validate_and_show(policy, table);
162 if (ret) {
163 pr_err("%s: invalid frequency table: %d\n", __func__, ret);
164 return ret;
165 }
166
167 policy->cpuinfo.transition_latency = transition_latency;
168
169 /*
170 * The driver only supports the SMP configuartion where all processors
171 * share the clock and voltage and clock.
172 */
173 cpumask_setall(policy->cpus);
174
175 return 0;
176}
177EXPORT_SYMBOL_GPL(cpufreq_generic_init);
178
Viresh Kumar652ed952014-01-09 20:38:43 +0530179unsigned int cpufreq_generic_get(unsigned int cpu)
180{
181 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
182
183 if (!policy || IS_ERR(policy->clk)) {
184 pr_err("%s: No %s associated to cpu: %d\n", __func__,
185 policy ? "clk" : "policy", cpu);
186 return 0;
187 }
188
189 return clk_get_rate(policy->clk) / 1000;
190}
191EXPORT_SYMBOL_GPL(cpufreq_generic_get);
192
Viresh Kumar6eed9402013-08-06 22:53:11 +0530193struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530195 struct cpufreq_policy *policy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 unsigned long flags;
197
Viresh Kumar6eed9402013-08-06 22:53:11 +0530198 if (cpufreq_disabled() || (cpu >= nr_cpu_ids))
199 return NULL;
200
201 if (!down_read_trylock(&cpufreq_rwsem))
202 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 /* get the cpufreq driver */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000205 read_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Viresh Kumar6eed9402013-08-06 22:53:11 +0530207 if (cpufreq_driver) {
208 /* get the CPU */
209 policy = per_cpu(cpufreq_cpu_data, cpu);
210 if (policy)
211 kobject_get(&policy->kobj);
212 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200213
Viresh Kumar6eed9402013-08-06 22:53:11 +0530214 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530216 if (!policy)
Viresh Kumar6eed9402013-08-06 22:53:11 +0530217 up_read(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530219 return policy;
Stephen Boyda9144432012-07-20 18:14:38 +0000220}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
222
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530223void cpufreq_cpu_put(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000225 if (cpufreq_disabled())
226 return;
227
Viresh Kumar6eed9402013-08-06 22:53:11 +0530228 kobject_put(&policy->kobj);
229 up_read(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
235 *********************************************************************/
236
237/**
238 * adjust_jiffies - adjust the system "loops_per_jiffy"
239 *
240 * This function alters the system "loops_per_jiffy" for the clock
241 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500242 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 * per-CPU loops_per_jiffy value wherever possible.
244 */
245#ifndef CONFIG_SMP
246static unsigned long l_p_j_ref;
Viresh Kumarbb176f72013-06-19 14:19:33 +0530247static unsigned int l_p_j_ref_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Arjan van de Ven858119e2006-01-14 13:20:43 -0800249static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 if (ci->flags & CPUFREQ_CONST_LOOPS)
252 return;
253
254 if (!l_p_j_ref_freq) {
255 l_p_j_ref = loops_per_jiffy;
256 l_p_j_ref_freq = ci->old;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200257 pr_debug("saving %lu as reference value for loops_per_jiffy; "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530258 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
Viresh Kumarbb176f72013-06-19 14:19:33 +0530260 if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700261 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530262 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
263 ci->new);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200264 pr_debug("scaling loops_per_jiffy to %lu "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530265 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267}
268#else
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530269static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
270{
271 return;
272}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273#endif
274
Viresh Kumar0956df9c2013-06-19 14:19:34 +0530275static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530276 struct cpufreq_freqs *freqs, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 BUG_ON(irqs_disabled());
279
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000280 if (cpufreq_disabled())
281 return;
282
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200283 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200284 pr_debug("notification %u of frequency transition to %u kHz\n",
Dave Jonese4472cb2006-01-31 15:53:55 -0800285 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500290 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800291 * which is not equal to what the cpufreq core thinks is
292 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200294 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800295 if ((policy) && (policy->cpu == freqs->cpu) &&
296 (policy->cur) && (policy->cur != freqs->old)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200297 pr_debug("Warning: CPU frequency is"
Dave Jonese4472cb2006-01-31 15:53:55 -0800298 " %u, cpufreq assumed %u kHz.\n",
299 freqs->old, policy->cur);
300 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700303 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800304 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
306 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 case CPUFREQ_POSTCHANGE:
309 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200310 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
Thomas Renninger6f4f2722010-04-20 13:17:36 +0200311 (unsigned long)freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100312 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700313 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800314 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800315 if (likely(policy) && likely(policy->cpu == freqs->cpu))
316 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 break;
318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
Viresh Kumarbb176f72013-06-19 14:19:33 +0530320
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530321/**
322 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
323 * on frequency transition.
324 *
325 * This function calls the transition notifiers and the "adjust_jiffies"
326 * function. It is called twice on all CPU frequency changes that have
327 * external effects.
328 */
329void cpufreq_notify_transition(struct cpufreq_policy *policy,
330 struct cpufreq_freqs *freqs, unsigned int state)
331{
332 for_each_cpu(freqs->cpu, policy->cpus)
333 __cpufreq_notify_transition(policy, freqs, state);
334}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
336
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530337/* Do post notifications when there are chances that transition has failed */
338void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
339 struct cpufreq_freqs *freqs, int transition_failed)
340{
341 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
342 if (!transition_failed)
343 return;
344
345 swap(freqs->old, freqs->new);
346 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
347 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
348}
349EXPORT_SYMBOL_GPL(cpufreq_notify_post_transition);
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352/*********************************************************************
353 * SYSFS INTERFACE *
354 *********************************************************************/
355
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700356static struct cpufreq_governor *__find_governor(const char *str_governor)
357{
358 struct cpufreq_governor *t;
359
360 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
Dave Jones29464f22009-01-18 01:37:11 -0500361 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700362 return t;
363
364 return NULL;
365}
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367/**
368 * cpufreq_parse_governor - parse a governor string
369 */
Dave Jones905d77c2008-03-05 14:28:32 -0500370static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 struct cpufreq_governor **governor)
372{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700373 int err = -EINVAL;
374
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200375 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700376 goto out;
377
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200378 if (cpufreq_driver->setpolicy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
380 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700381 err = 0;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530382 } else if (!strnicmp(str_governor, "powersave",
383 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700385 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530387 } else if (has_target()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700389
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800390 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700391
392 t = __find_governor(str_governor);
393
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700394 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700395 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700396
Kees Cook1a8e1462011-05-04 08:38:56 -0700397 mutex_unlock(&cpufreq_governor_mutex);
398 ret = request_module("cpufreq_%s", str_governor);
399 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700400
Kees Cook1a8e1462011-05-04 08:38:56 -0700401 if (ret == 0)
402 t = __find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700403 }
404
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700405 if (t != NULL) {
406 *governor = t;
407 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700409
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800410 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
Dave Jones29464f22009-01-18 01:37:11 -0500412out:
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700413 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530417 * cpufreq_per_cpu_attr_read() / show_##file_name() -
418 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 *
420 * Write out information from cpufreq_driver->policy[cpu]; object must be
421 * "unsigned int".
422 */
423
Dave Jones32ee8c32006-02-28 00:43:23 -0500424#define show_one(file_name, object) \
425static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500426(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500427{ \
Dave Jones29464f22009-01-18 01:37:11 -0500428 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
431show_one(cpuinfo_min_freq, cpuinfo.min_freq);
432show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100433show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434show_one(scaling_min_freq, min);
435show_one(scaling_max_freq, max);
436show_one(scaling_cur_freq, cur);
437
Viresh Kumar037ce832013-10-02 14:13:16 +0530438static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530439 struct cpufreq_policy *new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441/**
442 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
443 */
444#define store_one(file_name, object) \
445static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500446(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{ \
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530448 int ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 struct cpufreq_policy new_policy; \
450 \
451 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
452 if (ret) \
453 return -EINVAL; \
454 \
Dave Jones29464f22009-01-18 01:37:11 -0500455 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (ret != 1) \
457 return -EINVAL; \
458 \
Viresh Kumar037ce832013-10-02 14:13:16 +0530459 ret = cpufreq_set_policy(policy, &new_policy); \
Thomas Renninger7970e082006-04-13 15:14:04 +0200460 policy->user_policy.object = policy->object; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 \
462 return ret ? ret : count; \
463}
464
Dave Jones29464f22009-01-18 01:37:11 -0500465store_one(scaling_min_freq, min);
466store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468/**
469 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
470 */
Dave Jones905d77c2008-03-05 14:28:32 -0500471static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
472 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800474 unsigned int cur_freq = __cpufreq_get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (!cur_freq)
476 return sprintf(buf, "<unknown>");
477 return sprintf(buf, "%u\n", cur_freq);
478}
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480/**
481 * show_scaling_governor - show the current policy for the specified CPU
482 */
Dave Jones905d77c2008-03-05 14:28:32 -0500483static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Dave Jones29464f22009-01-18 01:37:11 -0500485 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return sprintf(buf, "powersave\n");
487 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
488 return sprintf(buf, "performance\n");
489 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200490 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500491 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return -EINVAL;
493}
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495/**
496 * store_scaling_governor - store policy for the specified CPU
497 */
Dave Jones905d77c2008-03-05 14:28:32 -0500498static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
499 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530501 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 char str_governor[16];
503 struct cpufreq_policy new_policy;
504
505 ret = cpufreq_get_policy(&new_policy, policy->cpu);
506 if (ret)
507 return ret;
508
Dave Jones29464f22009-01-18 01:37:11 -0500509 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (ret != 1)
511 return -EINVAL;
512
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530513 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
514 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return -EINVAL;
516
Viresh Kumar037ce832013-10-02 14:13:16 +0530517 ret = cpufreq_set_policy(policy, &new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200518
519 policy->user_policy.policy = policy->policy;
520 policy->user_policy.governor = policy->governor;
Thomas Renninger7970e082006-04-13 15:14:04 +0200521
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530522 if (ret)
523 return ret;
524 else
525 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
528/**
529 * show_scaling_driver - show the cpufreq driver currently loaded
530 */
Dave Jones905d77c2008-03-05 14:28:32 -0500531static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200533 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
536/**
537 * show_scaling_available_governors - show the available CPUfreq governors
538 */
Dave Jones905d77c2008-03-05 14:28:32 -0500539static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
540 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 ssize_t i = 0;
543 struct cpufreq_governor *t;
544
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530545 if (!has_target()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 i += sprintf(buf, "performance powersave");
547 goto out;
548 }
549
550 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
Dave Jones29464f22009-01-18 01:37:11 -0500551 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
552 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200554 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500556out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 i += sprintf(&buf[i], "\n");
558 return i;
559}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700560
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800561ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 ssize_t i = 0;
564 unsigned int cpu;
565
Rusty Russell835481d2009-01-04 05:18:06 -0800566 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (i)
568 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
569 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
570 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500571 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
573 i += sprintf(&buf[i], "\n");
574 return i;
575}
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800576EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700578/**
579 * show_related_cpus - show the CPUs affected by each transition even if
580 * hw coordination is in use
581 */
582static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
583{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800584 return cpufreq_show_cpus(policy->related_cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700585}
586
587/**
588 * show_affected_cpus - show the CPUs affected by each transition
589 */
590static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
591{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800592 return cpufreq_show_cpus(policy->cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700593}
594
Venki Pallipadi9e769882007-10-26 10:18:21 -0700595static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500596 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700597{
598 unsigned int freq = 0;
599 unsigned int ret;
600
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700601 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700602 return -EINVAL;
603
604 ret = sscanf(buf, "%u", &freq);
605 if (ret != 1)
606 return -EINVAL;
607
608 policy->governor->store_setspeed(policy, freq);
609
610 return count;
611}
612
613static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
614{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700615 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700616 return sprintf(buf, "<unsupported>\n");
617
618 return policy->governor->show_setspeed(policy, buf);
619}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Thomas Renningere2f74f32009-11-19 12:31:01 +0100621/**
viresh kumar8bf1ac722012-10-23 01:23:33 +0200622 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100623 */
624static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
625{
626 unsigned int limit;
627 int ret;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200628 if (cpufreq_driver->bios_limit) {
629 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
Thomas Renningere2f74f32009-11-19 12:31:01 +0100630 if (!ret)
631 return sprintf(buf, "%u\n", limit);
632 }
633 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
634}
635
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200636cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
637cpufreq_freq_attr_ro(cpuinfo_min_freq);
638cpufreq_freq_attr_ro(cpuinfo_max_freq);
639cpufreq_freq_attr_ro(cpuinfo_transition_latency);
640cpufreq_freq_attr_ro(scaling_available_governors);
641cpufreq_freq_attr_ro(scaling_driver);
642cpufreq_freq_attr_ro(scaling_cur_freq);
643cpufreq_freq_attr_ro(bios_limit);
644cpufreq_freq_attr_ro(related_cpus);
645cpufreq_freq_attr_ro(affected_cpus);
646cpufreq_freq_attr_rw(scaling_min_freq);
647cpufreq_freq_attr_rw(scaling_max_freq);
648cpufreq_freq_attr_rw(scaling_governor);
649cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Dave Jones905d77c2008-03-05 14:28:32 -0500651static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 &cpuinfo_min_freq.attr,
653 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100654 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 &scaling_min_freq.attr,
656 &scaling_max_freq.attr,
657 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700658 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 &scaling_governor.attr,
660 &scaling_driver.attr,
661 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700662 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 NULL
664};
665
Dave Jones29464f22009-01-18 01:37:11 -0500666#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
667#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Dave Jones29464f22009-01-18 01:37:11 -0500669static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Dave Jones905d77c2008-03-05 14:28:32 -0500671 struct cpufreq_policy *policy = to_policy(kobj);
672 struct freq_attr *fattr = to_attr(attr);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530673 ssize_t ret;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530674
675 if (!down_read_trylock(&cpufreq_rwsem))
Viresh Kumar1b750e32013-10-02 14:13:09 +0530676 return -EINVAL;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800677
viresh kumarad7722d2013-10-18 19:10:15 +0530678 down_read(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800679
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530680 if (fattr->show)
681 ret = fattr->show(policy, buf);
682 else
683 ret = -EIO;
684
viresh kumarad7722d2013-10-18 19:10:15 +0530685 up_read(&policy->rwsem);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530686 up_read(&cpufreq_rwsem);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return ret;
689}
690
Dave Jones905d77c2008-03-05 14:28:32 -0500691static ssize_t store(struct kobject *kobj, struct attribute *attr,
692 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Dave Jones905d77c2008-03-05 14:28:32 -0500694 struct cpufreq_policy *policy = to_policy(kobj);
695 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500696 ssize_t ret = -EINVAL;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530697
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530698 get_online_cpus();
699
700 if (!cpu_online(policy->cpu))
701 goto unlock;
702
Viresh Kumar6eed9402013-08-06 22:53:11 +0530703 if (!down_read_trylock(&cpufreq_rwsem))
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530704 goto unlock;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800705
viresh kumarad7722d2013-10-18 19:10:15 +0530706 down_write(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800707
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530708 if (fattr->store)
709 ret = fattr->store(policy, buf, count);
710 else
711 ret = -EIO;
712
viresh kumarad7722d2013-10-18 19:10:15 +0530713 up_write(&policy->rwsem);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530714
Viresh Kumar6eed9402013-08-06 22:53:11 +0530715 up_read(&cpufreq_rwsem);
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530716unlock:
717 put_online_cpus();
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return ret;
720}
721
Dave Jones905d77c2008-03-05 14:28:32 -0500722static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Dave Jones905d77c2008-03-05 14:28:32 -0500724 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200725 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 complete(&policy->kobj_unregister);
727}
728
Emese Revfy52cf25d2010-01-19 02:58:23 +0100729static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 .show = show,
731 .store = store,
732};
733
734static struct kobj_type ktype_cpufreq = {
735 .sysfs_ops = &sysfs_ops,
736 .default_attrs = default_attrs,
737 .release = cpufreq_sysfs_release,
738};
739
Viresh Kumar2361be22013-05-17 16:09:09 +0530740struct kobject *cpufreq_global_kobject;
741EXPORT_SYMBOL(cpufreq_global_kobject);
742
743static int cpufreq_global_kobject_usage;
744
745int cpufreq_get_global_kobject(void)
746{
747 if (!cpufreq_global_kobject_usage++)
748 return kobject_add(cpufreq_global_kobject,
749 &cpu_subsys.dev_root->kobj, "%s", "cpufreq");
750
751 return 0;
752}
753EXPORT_SYMBOL(cpufreq_get_global_kobject);
754
755void cpufreq_put_global_kobject(void)
756{
757 if (!--cpufreq_global_kobject_usage)
758 kobject_del(cpufreq_global_kobject);
759}
760EXPORT_SYMBOL(cpufreq_put_global_kobject);
761
762int cpufreq_sysfs_create_file(const struct attribute *attr)
763{
764 int ret = cpufreq_get_global_kobject();
765
766 if (!ret) {
767 ret = sysfs_create_file(cpufreq_global_kobject, attr);
768 if (ret)
769 cpufreq_put_global_kobject();
770 }
771
772 return ret;
773}
774EXPORT_SYMBOL(cpufreq_sysfs_create_file);
775
776void cpufreq_sysfs_remove_file(const struct attribute *attr)
777{
778 sysfs_remove_file(cpufreq_global_kobject, attr);
779 cpufreq_put_global_kobject();
780}
781EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
782
Dave Jones19d6f7e2009-07-08 17:35:39 -0400783/* symlink affected CPUs */
Viresh Kumar308b60e2013-07-31 14:35:14 +0200784static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400785{
786 unsigned int j;
787 int ret = 0;
788
789 for_each_cpu(j, policy->cpus) {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800790 struct device *cpu_dev;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400791
Viresh Kumar308b60e2013-07-31 14:35:14 +0200792 if (j == policy->cpu)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400793 continue;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400794
Viresh Kumare8fdde12013-07-31 14:31:33 +0200795 pr_debug("Adding link for CPU: %u\n", j);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800796 cpu_dev = get_cpu_device(j);
797 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Dave Jones19d6f7e2009-07-08 17:35:39 -0400798 "cpufreq");
Rafael J. Wysocki71c34612013-08-04 01:19:34 +0200799 if (ret)
800 break;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400801 }
802 return ret;
803}
804
Viresh Kumar308b60e2013-07-31 14:35:14 +0200805static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800806 struct device *dev)
Dave Jones909a6942009-07-08 18:05:42 -0400807{
808 struct freq_attr **drv_attr;
Dave Jones909a6942009-07-08 18:05:42 -0400809 int ret = 0;
Dave Jones909a6942009-07-08 18:05:42 -0400810
811 /* prepare interface data */
812 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800813 &dev->kobj, "cpufreq");
Dave Jones909a6942009-07-08 18:05:42 -0400814 if (ret)
815 return ret;
816
817 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200818 drv_attr = cpufreq_driver->attr;
Dave Jones909a6942009-07-08 18:05:42 -0400819 while ((drv_attr) && (*drv_attr)) {
820 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
821 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200822 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400823 drv_attr++;
824 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200825 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -0400826 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
827 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200828 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400829 }
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530830 if (has_target()) {
Dave Jones909a6942009-07-08 18:05:42 -0400831 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
832 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200833 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400834 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200835 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +0100836 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
837 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200838 goto err_out_kobj_put;
Thomas Renningere2f74f32009-11-19 12:31:01 +0100839 }
Dave Jones909a6942009-07-08 18:05:42 -0400840
Viresh Kumar308b60e2013-07-31 14:35:14 +0200841 ret = cpufreq_add_dev_symlink(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400842 if (ret)
843 goto err_out_kobj_put;
844
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530845 return ret;
846
847err_out_kobj_put:
848 kobject_put(&policy->kobj);
849 wait_for_completion(&policy->kobj_unregister);
850 return ret;
851}
852
853static void cpufreq_init_policy(struct cpufreq_policy *policy)
854{
855 struct cpufreq_policy new_policy;
856 int ret = 0;
857
Viresh Kumard5b73cd2013-08-06 22:53:06 +0530858 memcpy(&new_policy, policy, sizeof(*policy));
Jason Barona27a9ab2013-12-19 22:50:50 +0000859
860 /* Use the default policy if its valid. */
861 if (cpufreq_driver->setpolicy)
862 cpufreq_parse_governor(policy->governor->name,
863 &new_policy.policy, NULL);
864
Viresh Kumar037ce832013-10-02 14:13:16 +0530865 /* assure that the starting sequence is run in cpufreq_set_policy */
Dave Jonesecf7e462009-07-08 18:48:47 -0400866 policy->governor = NULL;
867
868 /* set default policy */
Viresh Kumar037ce832013-10-02 14:13:16 +0530869 ret = cpufreq_set_policy(policy, &new_policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400870 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200871 pr_debug("setting policy failed\n");
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200872 if (cpufreq_driver->exit)
873 cpufreq_driver->exit(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400874 }
Dave Jones909a6942009-07-08 18:05:42 -0400875}
876
Viresh Kumarfcf80582013-01-29 14:39:08 +0000877#ifdef CONFIG_HOTPLUG_CPU
Viresh Kumard8d3b472013-08-04 01:20:07 +0200878static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,
Viresh Kumar42f921a2013-12-20 21:26:02 +0530879 unsigned int cpu, struct device *dev)
Viresh Kumarfcf80582013-01-29 14:39:08 +0000880{
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530881 int ret = 0;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000882 unsigned long flags;
883
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530884 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +0530885 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
886 if (ret) {
887 pr_err("%s: Failed to stop governor\n", __func__);
888 return ret;
889 }
890 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000891
viresh kumarad7722d2013-10-18 19:10:15 +0530892 down_write(&policy->rwsem);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530893
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000894 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530895
Viresh Kumarfcf80582013-01-29 14:39:08 +0000896 cpumask_set_cpu(cpu, policy->cpus);
897 per_cpu(cpufreq_cpu_data, cpu) = policy;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000898 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000899
viresh kumarad7722d2013-10-18 19:10:15 +0530900 up_write(&policy->rwsem);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530901
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530902 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +0530903 if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
904 (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
905 pr_err("%s: Failed to start governor\n", __func__);
906 return ret;
907 }
Viresh Kumar820c6ca2013-04-22 00:48:03 +0200908 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000909
Viresh Kumar42f921a2013-12-20 21:26:02 +0530910 return sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
Viresh Kumarfcf80582013-01-29 14:39:08 +0000911}
912#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530914static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu)
915{
916 struct cpufreq_policy *policy;
917 unsigned long flags;
918
Lan Tianyu44871c92013-09-11 15:05:05 +0800919 read_lock_irqsave(&cpufreq_driver_lock, flags);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530920
921 policy = per_cpu(cpufreq_cpu_data_fallback, cpu);
922
Lan Tianyu44871c92013-09-11 15:05:05 +0800923 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530924
925 return policy;
926}
927
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +0530928static struct cpufreq_policy *cpufreq_policy_alloc(void)
929{
930 struct cpufreq_policy *policy;
931
932 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
933 if (!policy)
934 return NULL;
935
936 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
937 goto err_free_policy;
938
939 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
940 goto err_free_cpumask;
941
Lukasz Majewskic88a1f82013-08-06 22:53:08 +0530942 INIT_LIST_HEAD(&policy->policy_list);
viresh kumarad7722d2013-10-18 19:10:15 +0530943 init_rwsem(&policy->rwsem);
944
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +0530945 return policy;
946
947err_free_cpumask:
948 free_cpumask_var(policy->cpus);
949err_free_policy:
950 kfree(policy);
951
952 return NULL;
953}
954
Viresh Kumar42f921a2013-12-20 21:26:02 +0530955static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
956{
957 struct kobject *kobj;
958 struct completion *cmp;
959
Viresh Kumarfcd7af92014-01-07 07:10:10 +0530960 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
961 CPUFREQ_REMOVE_POLICY, policy);
962
Viresh Kumar42f921a2013-12-20 21:26:02 +0530963 down_read(&policy->rwsem);
964 kobj = &policy->kobj;
965 cmp = &policy->kobj_unregister;
966 up_read(&policy->rwsem);
967 kobject_put(kobj);
968
969 /*
970 * We need to make sure that the underlying kobj is
971 * actually not referenced anymore by anybody before we
972 * proceed with unloading.
973 */
974 pr_debug("waiting for dropping of refcount\n");
975 wait_for_completion(cmp);
976 pr_debug("wait complete\n");
977}
978
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +0530979static void cpufreq_policy_free(struct cpufreq_policy *policy)
980{
981 free_cpumask_var(policy->related_cpus);
982 free_cpumask_var(policy->cpus);
983 kfree(policy);
984}
985
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +0530986static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
987{
Srivatsa S. Bhat99ec8992013-09-12 17:29:09 +0530988 if (WARN_ON(cpu == policy->cpu))
Srivatsa S. Bhatcb38ed52013-09-12 01:43:42 +0530989 return;
990
viresh kumarad7722d2013-10-18 19:10:15 +0530991 down_write(&policy->rwsem);
Viresh Kumar8efd5762013-09-17 10:22:11 +0530992
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +0530993 policy->last_cpu = policy->cpu;
994 policy->cpu = cpu;
995
viresh kumarad7722d2013-10-18 19:10:15 +0530996 up_write(&policy->rwsem);
Viresh Kumar8efd5762013-09-17 10:22:11 +0530997
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +0530998 cpufreq_frequency_table_update_policy_cpu(policy);
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +0530999 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1000 CPUFREQ_UPDATE_POLICY_CPU, policy);
1001}
1002
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301003static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif,
1004 bool frozen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
Viresh Kumarfcf80582013-01-29 14:39:08 +00001006 unsigned int j, cpu = dev->id;
Viresh Kumar65922462013-02-07 10:56:03 +05301007 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 unsigned long flags;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001010#ifdef CONFIG_HOTPLUG_CPU
Viresh Kumar1b274292013-08-20 12:08:26 +05301011 struct cpufreq_policy *tpolicy;
Viresh Kumarfcf80582013-01-29 14:39:08 +00001012 struct cpufreq_governor *gov;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001013#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Ashok Rajc32b6b82005-10-30 14:59:54 -08001015 if (cpu_is_offline(cpu))
1016 return 0;
1017
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001018 pr_debug("adding CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020#ifdef CONFIG_SMP
1021 /* check whether a different CPU already registered this
1022 * CPU because it is in the same boat. */
1023 policy = cpufreq_cpu_get(cpu);
1024 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -05001025 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return 0;
1027 }
Li Zhong5025d622013-08-21 01:31:08 +02001028#endif
Viresh Kumarfcf80582013-01-29 14:39:08 +00001029
Viresh Kumar6eed9402013-08-06 22:53:11 +05301030 if (!down_read_trylock(&cpufreq_rwsem))
1031 return 0;
1032
Viresh Kumarfcf80582013-01-29 14:39:08 +00001033#ifdef CONFIG_HOTPLUG_CPU
1034 /* Check if this cpu was hot-unplugged earlier and has siblings */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001035 read_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar1b274292013-08-20 12:08:26 +05301036 list_for_each_entry(tpolicy, &cpufreq_policy_list, policy_list) {
1037 if (cpumask_test_cpu(cpu, tpolicy->related_cpus)) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001038 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301039 ret = cpufreq_add_policy_cpu(tpolicy, cpu, dev);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301040 up_read(&cpufreq_rwsem);
1041 return ret;
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301042 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001043 }
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001044 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +00001045#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001047 /*
1048 * Restore the saved policy when doing light-weight init and fall back
1049 * to the full init if that fails.
1050 */
1051 policy = frozen ? cpufreq_policy_restore(cpu) : NULL;
1052 if (!policy) {
1053 frozen = false;
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301054 policy = cpufreq_policy_alloc();
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001055 if (!policy)
1056 goto nomem_out;
1057 }
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301058
1059 /*
1060 * In the resume path, since we restore a saved policy, the assignment
1061 * to policy->cpu is like an update of the existing policy, rather than
1062 * the creation of a brand new one. So we need to perform this update
1063 * by invoking update_policy_cpu().
1064 */
1065 if (frozen && cpu != policy->cpu)
1066 update_policy_cpu(policy, cpu);
1067 else
1068 policy->cpu = cpu;
1069
Viresh Kumar65922462013-02-07 10:56:03 +05301070 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Rusty Russell835481d2009-01-04 05:18:06 -08001071 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 init_completion(&policy->kobj_unregister);
David Howells65f27f32006-11-22 14:55:48 +00001074 INIT_WORK(&policy->update, handle_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 /* call driver. From then on the cpufreq must be able
1077 * to accept all calls to ->verify and ->setpolicy for this CPU
1078 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001079 ret = cpufreq_driver->init(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001081 pr_debug("initialization failed\n");
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301082 goto err_set_policy_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 }
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001084
Viresh Kumar652ed952014-01-09 20:38:43 +05301085 write_lock_irqsave(&cpufreq_driver_lock, flags);
1086 for_each_cpu(j, policy->cpus)
1087 per_cpu(cpufreq_cpu_data, j) = policy;
1088 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1089
Viresh Kumarda60ce92013-10-03 20:28:30 +05301090 if (cpufreq_driver->get) {
1091 policy->cur = cpufreq_driver->get(policy->cpu);
1092 if (!policy->cur) {
1093 pr_err("%s: ->get() failed\n", __func__);
1094 goto err_get_freq;
1095 }
1096 }
1097
Viresh Kumard3916692013-12-03 11:20:46 +05301098 /*
1099 * Sometimes boot loaders set CPU frequency to a value outside of
1100 * frequency table present with cpufreq core. In such cases CPU might be
1101 * unstable if it has to run on that frequency for long duration of time
1102 * and so its better to set it to a frequency which is specified in
1103 * freq-table. This also makes cpufreq stats inconsistent as
1104 * cpufreq-stats would fail to register because current frequency of CPU
1105 * isn't found in freq-table.
1106 *
1107 * Because we don't want this change to effect boot process badly, we go
1108 * for the next freq which is >= policy->cur ('cur' must be set by now,
1109 * otherwise we will end up setting freq to lowest of the table as 'cur'
1110 * is initialized to zero).
1111 *
1112 * We are passing target-freq as "policy->cur - 1" otherwise
1113 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1114 * equal to target-freq.
1115 */
1116 if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1117 && has_target()) {
1118 /* Are we running at unknown frequency ? */
1119 ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1120 if (ret == -EINVAL) {
1121 /* Warn user and fix it */
1122 pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1123 __func__, policy->cpu, policy->cur);
1124 ret = __cpufreq_driver_target(policy, policy->cur - 1,
1125 CPUFREQ_RELATION_L);
1126
1127 /*
1128 * Reaching here after boot in a few seconds may not
1129 * mean that system will remain stable at "unknown"
1130 * frequency for longer duration. Hence, a BUG_ON().
1131 */
1132 BUG_ON(ret);
1133 pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1134 __func__, policy->cpu, policy->cur);
1135 }
1136 }
1137
Viresh Kumarfcf80582013-01-29 14:39:08 +00001138 /* related cpus should atleast have policy->cpus */
1139 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
1140
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001141 /*
1142 * affected cpus must always be the one, which are online. We aren't
1143 * managing offline cpus here.
1144 */
1145 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1146
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301147 if (!frozen) {
1148 policy->user_policy.min = policy->min;
1149 policy->user_policy.max = policy->max;
1150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Thomas Renningera1531ac2008-07-29 22:32:58 -07001152 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1153 CPUFREQ_START, policy);
1154
Viresh Kumarfcf80582013-01-29 14:39:08 +00001155#ifdef CONFIG_HOTPLUG_CPU
1156 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
1157 if (gov) {
1158 policy->governor = gov;
1159 pr_debug("Restoring governor %s for cpu %d\n",
1160 policy->governor->name, cpu);
Thomas Renninger4bfa0422009-07-24 15:25:03 +02001161 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001162#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301164 if (!frozen) {
Viresh Kumar308b60e2013-07-31 14:35:14 +02001165 ret = cpufreq_add_dev_interface(policy, dev);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301166 if (ret)
1167 goto err_out_unregister;
Viresh Kumarfcd7af92014-01-07 07:10:10 +05301168 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1169 CPUFREQ_CREATE_POLICY, policy);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301170 }
Dave Jones8ff69732006-03-05 03:37:23 -05001171
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301172 write_lock_irqsave(&cpufreq_driver_lock, flags);
1173 list_add(&policy->policy_list, &cpufreq_policy_list);
1174 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1175
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301176 cpufreq_init_policy(policy);
1177
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301178 if (!frozen) {
1179 policy->user_policy.policy = policy->policy;
1180 policy->user_policy.governor = policy->governor;
1181 }
1182
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001183 kobject_uevent(&policy->kobj, KOBJ_ADD);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301184 up_read(&cpufreq_rwsem);
1185
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001186 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 return 0;
1189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190err_out_unregister:
Viresh Kumar652ed952014-01-09 20:38:43 +05301191err_get_freq:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001192 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar474deff2013-08-20 12:08:25 +05301193 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -07001194 per_cpu(cpufreq_cpu_data, j) = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001195 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Viresh Kumarda60ce92013-10-03 20:28:30 +05301197 if (cpufreq_driver->exit)
1198 cpufreq_driver->exit(policy);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301199err_set_policy_cpu:
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001200 if (frozen) {
1201 /* Do not leave stale fallback data behind. */
1202 per_cpu(cpufreq_cpu_data_fallback, cpu) = NULL;
Viresh Kumar42f921a2013-12-20 21:26:02 +05301203 cpufreq_policy_put_kobj(policy);
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001204 }
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301205 cpufreq_policy_free(policy);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207nomem_out:
Viresh Kumar6eed9402013-08-06 22:53:11 +05301208 up_read(&cpufreq_rwsem);
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 return ret;
1211}
1212
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301213/**
1214 * cpufreq_add_dev - add a CPU device
1215 *
1216 * Adds the cpufreq interface for a CPU device.
1217 *
1218 * The Oracle says: try running cpufreq registration/unregistration concurrently
1219 * with with cpu hotplugging and all hell will break loose. Tried to clean this
1220 * mess up, but more thorough testing is needed. - Mathieu
1221 */
1222static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1223{
1224 return __cpufreq_add_dev(dev, sif, false);
1225}
1226
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301227static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy *policy,
Viresh Kumar42f921a2013-12-20 21:26:02 +05301228 unsigned int old_cpu)
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301229{
1230 struct device *cpu_dev;
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301231 int ret;
1232
1233 /* first sibling now owns the new sysfs dir */
Viresh Kumar9c8f1ee2013-09-12 17:06:33 +05301234 cpu_dev = get_cpu_device(cpumask_any_but(policy->cpus, old_cpu));
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301235
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301236 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301237 ret = kobject_move(&policy->kobj, &cpu_dev->kobj);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301238 if (ret) {
1239 pr_err("%s: Failed to move kobj: %d", __func__, ret);
1240
viresh kumarad7722d2013-10-18 19:10:15 +05301241 down_write(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301242 cpumask_set_cpu(old_cpu, policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301243 up_write(&policy->rwsem);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301244
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301245 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301246 "cpufreq");
1247
1248 return -EINVAL;
1249 }
1250
1251 return cpu_dev->id;
1252}
1253
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301254static int __cpufreq_remove_dev_prepare(struct device *dev,
1255 struct subsys_interface *sif,
1256 bool frozen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301258 unsigned int cpu = dev->id, cpus;
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301259 int new_cpu, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 unsigned long flags;
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301261 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001263 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001265 write_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301267 policy = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301269 /* Save the policy somewhere when doing a light-weight tear-down */
1270 if (frozen)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301271 per_cpu(cpufreq_cpu_data_fallback, cpu) = policy;
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301272
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001273 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301275 if (!policy) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001276 pr_debug("%s: No cpu_data found\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301280 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301281 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1282 if (ret) {
1283 pr_err("%s: Failed to stop governor\n", __func__);
1284 return ret;
1285 }
1286 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001287
Jacob Shin27ecddc2011-04-27 13:32:11 -05001288#ifdef CONFIG_HOTPLUG_CPU
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001289 if (!cpufreq_driver->setpolicy)
Dirk Brandewiefa69e332013-02-06 09:02:11 -08001290 strncpy(per_cpu(cpufreq_cpu_governor, cpu),
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301291 policy->governor->name, CPUFREQ_NAME_LEN);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001292#endif
1293
viresh kumarad7722d2013-10-18 19:10:15 +05301294 down_read(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301295 cpus = cpumask_weight(policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301296 up_read(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Srivatsa S. Bhat61173f22013-09-12 01:43:25 +05301298 if (cpu != policy->cpu) {
1299 if (!frozen)
1300 sysfs_remove_link(&dev->kobj, "cpufreq");
Viresh Kumar73bf0fc2013-02-05 22:21:14 +01001301 } else if (cpus > 1) {
Viresh Kumar42f921a2013-12-20 21:26:02 +05301302 new_cpu = cpufreq_nominate_new_policy_cpu(policy, cpu);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301303 if (new_cpu >= 0) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301304 update_policy_cpu(policy, new_cpu);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301305
1306 if (!frozen) {
Viresh Kumar75949c92013-10-02 14:13:13 +05301307 pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
1308 __func__, new_cpu, cpu);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301309 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001310 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001311 }
Venki Pallipadiec282972007-03-26 12:03:19 -07001312
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301313 return 0;
1314}
1315
1316static int __cpufreq_remove_dev_finish(struct device *dev,
1317 struct subsys_interface *sif,
1318 bool frozen)
1319{
1320 unsigned int cpu = dev->id, cpus;
1321 int ret;
1322 unsigned long flags;
1323 struct cpufreq_policy *policy;
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301324
1325 read_lock_irqsave(&cpufreq_driver_lock, flags);
1326 policy = per_cpu(cpufreq_cpu_data, cpu);
1327 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1328
1329 if (!policy) {
1330 pr_debug("%s: No cpu_data found\n", __func__);
1331 return -EINVAL;
1332 }
1333
viresh kumarad7722d2013-10-18 19:10:15 +05301334 down_write(&policy->rwsem);
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301335 cpus = cpumask_weight(policy->cpus);
Viresh Kumar9c8f1ee2013-09-12 17:06:33 +05301336
1337 if (cpus > 1)
1338 cpumask_clear_cpu(cpu, policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301339 up_write(&policy->rwsem);
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301340
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001341 /* If cpu is last user of policy, free policy */
1342 if (cpus == 1) {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301343 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301344 ret = __cpufreq_governor(policy,
1345 CPUFREQ_GOV_POLICY_EXIT);
1346 if (ret) {
1347 pr_err("%s: Failed to exit governor\n",
1348 __func__);
1349 return ret;
1350 }
Viresh Kumaredab2fb2013-08-20 12:08:22 +05301351 }
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001352
Viresh Kumar42f921a2013-12-20 21:26:02 +05301353 if (!frozen)
1354 cpufreq_policy_put_kobj(policy);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301355
1356 /*
1357 * Perform the ->exit() even during light-weight tear-down,
1358 * since this is a core component, and is essential for the
1359 * subsequent light-weight ->init() to succeed.
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001360 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001361 if (cpufreq_driver->exit)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301362 cpufreq_driver->exit(policy);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001363
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301364 /* Remove policy from list of active policies */
1365 write_lock_irqsave(&cpufreq_driver_lock, flags);
1366 list_del(&policy->policy_list);
1367 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1368
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301369 if (!frozen)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301370 cpufreq_policy_free(policy);
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001371 } else {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301372 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301373 if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
1374 (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
1375 pr_err("%s: Failed to start governor\n",
1376 __func__);
1377 return ret;
1378 }
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001379 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Viresh Kumar474deff2013-08-20 12:08:25 +05301382 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 return 0;
1384}
1385
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301386/**
Viresh Kumar27a862e2013-10-02 14:13:14 +05301387 * cpufreq_remove_dev - remove a CPU device
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301388 *
1389 * Removes the cpufreq interface for a CPU device.
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301390 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001391static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001392{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001393 unsigned int cpu = dev->id;
Viresh Kumar27a862e2013-10-02 14:13:14 +05301394 int ret;
Venki Pallipadiec282972007-03-26 12:03:19 -07001395
1396 if (cpu_is_offline(cpu))
1397 return 0;
1398
Viresh Kumar27a862e2013-10-02 14:13:14 +05301399 ret = __cpufreq_remove_dev_prepare(dev, sif, false);
1400
1401 if (!ret)
1402 ret = __cpufreq_remove_dev_finish(dev, sif, false);
1403
1404 return ret;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001405}
1406
David Howells65f27f32006-11-22 14:55:48 +00001407static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
David Howells65f27f32006-11-22 14:55:48 +00001409 struct cpufreq_policy *policy =
1410 container_of(work, struct cpufreq_policy, update);
1411 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001412 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 cpufreq_update_policy(cpu);
1414}
1415
1416/**
Viresh Kumarbb176f72013-06-19 14:19:33 +05301417 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1418 * in deep trouble.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 * @cpu: cpu number
1420 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1421 * @new_freq: CPU frequency the CPU actually runs at
1422 *
Dave Jones29464f22009-01-18 01:37:11 -05001423 * We adjust to current frequency first, and need to clean up later.
1424 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301426static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1427 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301429 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301431 unsigned long flags;
1432
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001433 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 freqs.old = old_freq;
1437 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301438
1439 read_lock_irqsave(&cpufreq_driver_lock, flags);
1440 policy = per_cpu(cpufreq_cpu_data, cpu);
1441 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1442
1443 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1444 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445}
1446
Dave Jones32ee8c32006-02-28 00:43:23 -05001447/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301448 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001449 * @cpu: CPU number
1450 *
1451 * This is the last known freq, without actually getting it from the driver.
1452 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1453 */
1454unsigned int cpufreq_quick_get(unsigned int cpu)
1455{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001456 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301457 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001458
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001459 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1460 return cpufreq_driver->get(cpu);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001461
1462 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001463 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301464 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001465 cpufreq_cpu_put(policy);
1466 }
1467
Dave Jones4d34a672008-02-07 16:33:49 -05001468 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001469}
1470EXPORT_SYMBOL(cpufreq_quick_get);
1471
Jesse Barnes3d737102011-06-28 10:59:12 -07001472/**
1473 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1474 * @cpu: CPU number
1475 *
1476 * Just return the max possible frequency for a given CPU.
1477 */
1478unsigned int cpufreq_quick_get_max(unsigned int cpu)
1479{
1480 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1481 unsigned int ret_freq = 0;
1482
1483 if (policy) {
1484 ret_freq = policy->max;
1485 cpufreq_cpu_put(policy);
1486 }
1487
1488 return ret_freq;
1489}
1490EXPORT_SYMBOL(cpufreq_quick_get_max);
1491
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001492static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001494 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301495 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001497 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001498 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001500 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301502 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001503 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301504 /* verify no discrepancy between actual and
1505 saved value exists */
1506 if (unlikely(ret_freq != policy->cur)) {
1507 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 schedule_work(&policy->update);
1509 }
1510 }
1511
Dave Jones4d34a672008-02-07 16:33:49 -05001512 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001513}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001515/**
1516 * cpufreq_get - get the current CPU frequency (in kHz)
1517 * @cpu: CPU number
1518 *
1519 * Get the CPU current (static) CPU frequency
1520 */
1521unsigned int cpufreq_get(unsigned int cpu)
1522{
viresh kumarad7722d2013-10-18 19:10:15 +05301523 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001524 unsigned int ret_freq = 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001525
Viresh Kumar26ca8692013-09-20 22:37:31 +05301526 if (cpufreq_disabled() || !cpufreq_driver)
1527 return -ENOENT;
1528
viresh kumarad7722d2013-10-18 19:10:15 +05301529 BUG_ON(!policy);
1530
Viresh Kumar6eed9402013-08-06 22:53:11 +05301531 if (!down_read_trylock(&cpufreq_rwsem))
1532 return 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001533
viresh kumarad7722d2013-10-18 19:10:15 +05301534 down_read(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001535
1536 ret_freq = __cpufreq_get(cpu);
1537
viresh kumarad7722d2013-10-18 19:10:15 +05301538 up_read(&policy->rwsem);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301539 up_read(&cpufreq_rwsem);
1540
Dave Jones4d34a672008-02-07 16:33:49 -05001541 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542}
1543EXPORT_SYMBOL(cpufreq_get);
1544
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001545static struct subsys_interface cpufreq_interface = {
1546 .name = "cpufreq",
1547 .subsys = &cpu_subsys,
1548 .add_dev = cpufreq_add_dev,
1549 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001550};
1551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001553 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1554 *
1555 * This function is only executed for the boot processor. The other CPUs
1556 * have been put offline by means of CPU hotplug.
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001557 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001558static int cpufreq_bp_suspend(void)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001559{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301560 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001561
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001562 int cpu = smp_processor_id();
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301563 struct cpufreq_policy *policy;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001564
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001565 pr_debug("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001566
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001567 /* If there's no policy for the boot CPU, we have nothing to do. */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301568 policy = cpufreq_cpu_get(cpu);
1569 if (!policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001570 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001571
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001572 if (cpufreq_driver->suspend) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301573 ret = cpufreq_driver->suspend(policy);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001574 if (ret)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001575 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301576 "step on CPU %u\n", policy->cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001577 }
1578
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301579 cpufreq_cpu_put(policy);
Dave Jonesc9060492008-02-07 16:32:18 -05001580 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001581}
1582
1583/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001584 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 *
1586 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001587 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1588 * restored. It will verify that the current freq is in sync with
1589 * what we believe it to be. This is a bit later than when it
1590 * should be, but nonethteless it's better than calling
1591 * cpufreq_driver->get() here which might re-enable interrupts...
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001592 *
1593 * This function is only executed for the boot CPU. The other CPUs have not
1594 * been turned on yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001596static void cpufreq_bp_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301598 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001599
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001600 int cpu = smp_processor_id();
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301601 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001603 pr_debug("resuming cpu %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001605 /* If there's no policy for the boot CPU, we have nothing to do. */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301606 policy = cpufreq_cpu_get(cpu);
1607 if (!policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001608 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001610 if (cpufreq_driver->resume) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301611 ret = cpufreq_driver->resume(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 if (ret) {
1613 printk(KERN_ERR "cpufreq: resume failed in ->resume "
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301614 "step on CPU %u\n", policy->cpu);
Dave Jonesc9060492008-02-07 16:32:18 -05001615 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
1617 }
1618
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301619 schedule_work(&policy->update);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001620
Dave Jonesc9060492008-02-07 16:32:18 -05001621fail:
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301622 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623}
1624
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001625static struct syscore_ops cpufreq_syscore_ops = {
1626 .suspend = cpufreq_bp_suspend,
1627 .resume = cpufreq_bp_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628};
1629
Borislav Petkov9d950462013-01-20 10:24:28 +00001630/**
1631 * cpufreq_get_current_driver - return current driver's name
1632 *
1633 * Return the name string of the currently loaded cpufreq driver
1634 * or NULL, if none.
1635 */
1636const char *cpufreq_get_current_driver(void)
1637{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001638 if (cpufreq_driver)
1639 return cpufreq_driver->name;
1640
1641 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001642}
1643EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645/*********************************************************************
1646 * NOTIFIER LISTS INTERFACE *
1647 *********************************************************************/
1648
1649/**
1650 * cpufreq_register_notifier - register a driver with cpufreq
1651 * @nb: notifier function to register
1652 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1653 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001654 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 * are notified about clock rate changes (once before and once after
1656 * the transition), or a list of drivers that are notified about
1657 * changes in cpufreq policy.
1658 *
1659 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001660 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 */
1662int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1663{
1664 int ret;
1665
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001666 if (cpufreq_disabled())
1667 return -EINVAL;
1668
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001669 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 switch (list) {
1672 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001673 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001674 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 break;
1676 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001677 ret = blocking_notifier_chain_register(
1678 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 break;
1680 default:
1681 ret = -EINVAL;
1682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
1684 return ret;
1685}
1686EXPORT_SYMBOL(cpufreq_register_notifier);
1687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688/**
1689 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1690 * @nb: notifier block to be unregistered
Viresh Kumarbb176f72013-06-19 14:19:33 +05301691 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 *
1693 * Remove a driver from the CPU frequency notifier list.
1694 *
1695 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001696 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 */
1698int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1699{
1700 int ret;
1701
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001702 if (cpufreq_disabled())
1703 return -EINVAL;
1704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 switch (list) {
1706 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001707 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001708 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 break;
1710 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001711 ret = blocking_notifier_chain_unregister(
1712 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 break;
1714 default:
1715 ret = -EINVAL;
1716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
1718 return ret;
1719}
1720EXPORT_SYMBOL(cpufreq_unregister_notifier);
1721
1722
1723/*********************************************************************
1724 * GOVERNORS *
1725 *********************************************************************/
1726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727int __cpufreq_driver_target(struct cpufreq_policy *policy,
1728 unsigned int target_freq,
1729 unsigned int relation)
1730{
1731 int retval = -EINVAL;
Viresh Kumar72499242012-10-31 01:28:21 +01001732 unsigned int old_target_freq = target_freq;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001733
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001734 if (cpufreq_disabled())
1735 return -ENODEV;
1736
Viresh Kumar72499242012-10-31 01:28:21 +01001737 /* Make sure that target_freq is within supported range */
1738 if (target_freq > policy->max)
1739 target_freq = policy->max;
1740 if (target_freq < policy->min)
1741 target_freq = policy->min;
1742
1743 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1744 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001745
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301746 /*
1747 * This might look like a redundant call as we are checking it again
1748 * after finding index. But it is left intentionally for cases where
1749 * exactly same freq is called again and so we can save on few function
1750 * calls.
1751 */
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001752 if (target_freq == policy->cur)
1753 return 0;
1754
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001755 if (cpufreq_driver->target)
1756 retval = cpufreq_driver->target(policy, target_freq, relation);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301757 else if (cpufreq_driver->target_index) {
1758 struct cpufreq_frequency_table *freq_table;
Viresh Kumard4019f02013-08-14 19:38:24 +05301759 struct cpufreq_freqs freqs;
1760 bool notify;
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301761 int index;
Ashok Raj90d45d12005-11-08 21:34:24 -08001762
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301763 freq_table = cpufreq_frequency_get_table(policy->cpu);
1764 if (unlikely(!freq_table)) {
1765 pr_err("%s: Unable to find freq_table\n", __func__);
1766 goto out;
1767 }
1768
1769 retval = cpufreq_frequency_table_target(policy, freq_table,
1770 target_freq, relation, &index);
1771 if (unlikely(retval)) {
1772 pr_err("%s: Unable to find matching freq\n", __func__);
1773 goto out;
1774 }
1775
Viresh Kumard4019f02013-08-14 19:38:24 +05301776 if (freq_table[index].frequency == policy->cur) {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301777 retval = 0;
Viresh Kumard4019f02013-08-14 19:38:24 +05301778 goto out;
1779 }
1780
1781 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
1782
1783 if (notify) {
1784 freqs.old = policy->cur;
1785 freqs.new = freq_table[index].frequency;
1786 freqs.flags = 0;
1787
1788 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1789 __func__, policy->cpu, freqs.old,
1790 freqs.new);
1791
1792 cpufreq_notify_transition(policy, &freqs,
1793 CPUFREQ_PRECHANGE);
1794 }
1795
1796 retval = cpufreq_driver->target_index(policy, index);
1797 if (retval)
1798 pr_err("%s: Failed to change cpu frequency: %d\n",
1799 __func__, retval);
1800
Viresh Kumarab1b1c42013-12-02 11:04:13 +05301801 if (notify)
1802 cpufreq_notify_post_transition(policy, &freqs, retval);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301803 }
1804
1805out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 return retval;
1807}
1808EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1809
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810int cpufreq_driver_target(struct cpufreq_policy *policy,
1811 unsigned int target_freq,
1812 unsigned int relation)
1813{
Julia Lawallf1829e42008-07-25 22:44:53 +02001814 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
viresh kumarad7722d2013-10-18 19:10:15 +05301816 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
1818 ret = __cpufreq_driver_target(policy, target_freq, relation);
1819
viresh kumarad7722d2013-10-18 19:10:15 +05301820 up_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 return ret;
1823}
1824EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1825
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001826/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001827 * when "event" is CPUFREQ_GOV_LIMITS
1828 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301830static int __cpufreq_governor(struct cpufreq_policy *policy,
1831 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
Dave Jonescc993ca2005-07-28 09:43:56 -07001833 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001834
1835 /* Only must be defined when default governor is known to have latency
1836 restrictions, like e.g. conservative or ondemand.
1837 That this is the case is already ensured in Kconfig
1838 */
1839#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1840 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1841#else
1842 struct cpufreq_governor *gov = NULL;
1843#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001844
1845 if (policy->governor->max_transition_latency &&
1846 policy->cpuinfo.transition_latency >
1847 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07001848 if (!gov)
1849 return -EINVAL;
1850 else {
1851 printk(KERN_WARNING "%s governor failed, too long"
1852 " transition latency of HW, fallback"
1853 " to %s governor\n",
1854 policy->governor->name,
1855 gov->name);
1856 policy->governor = gov;
1857 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Viresh Kumarfe492f32013-08-06 22:53:10 +05301860 if (event == CPUFREQ_GOV_POLICY_INIT)
1861 if (!try_module_get(policy->governor->owner))
1862 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001864 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301865 policy->cpu, event);
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001866
1867 mutex_lock(&cpufreq_governor_lock);
Srivatsa S. Bhat56d07db2013-09-07 01:23:55 +05301868 if ((policy->governor_enabled && event == CPUFREQ_GOV_START)
Viresh Kumarf73d3932013-08-31 17:53:40 +05301869 || (!policy->governor_enabled
1870 && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) {
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001871 mutex_unlock(&cpufreq_governor_lock);
1872 return -EBUSY;
1873 }
1874
1875 if (event == CPUFREQ_GOV_STOP)
1876 policy->governor_enabled = false;
1877 else if (event == CPUFREQ_GOV_START)
1878 policy->governor_enabled = true;
1879
1880 mutex_unlock(&cpufreq_governor_lock);
1881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 ret = policy->governor->governor(policy, event);
1883
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001884 if (!ret) {
1885 if (event == CPUFREQ_GOV_POLICY_INIT)
1886 policy->governor->initialized++;
1887 else if (event == CPUFREQ_GOV_POLICY_EXIT)
1888 policy->governor->initialized--;
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001889 } else {
1890 /* Restore original values */
1891 mutex_lock(&cpufreq_governor_lock);
1892 if (event == CPUFREQ_GOV_STOP)
1893 policy->governor_enabled = true;
1894 else if (event == CPUFREQ_GOV_START)
1895 policy->governor_enabled = false;
1896 mutex_unlock(&cpufreq_governor_lock);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001897 }
Viresh Kumarb3940582013-02-01 05:42:58 +00001898
Viresh Kumarfe492f32013-08-06 22:53:10 +05301899 if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
1900 ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 module_put(policy->governor->owner);
1902
1903 return ret;
1904}
1905
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906int cpufreq_register_governor(struct cpufreq_governor *governor)
1907{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001908 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
1910 if (!governor)
1911 return -EINVAL;
1912
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001913 if (cpufreq_disabled())
1914 return -ENODEV;
1915
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001916 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001917
Viresh Kumarb3940582013-02-01 05:42:58 +00001918 governor->initialized = 0;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001919 err = -EBUSY;
1920 if (__find_governor(governor->name) == NULL) {
1921 err = 0;
1922 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Dave Jones32ee8c32006-02-28 00:43:23 -05001925 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001926 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927}
1928EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1929
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1931{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001932#ifdef CONFIG_HOTPLUG_CPU
1933 int cpu;
1934#endif
1935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 if (!governor)
1937 return;
1938
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001939 if (cpufreq_disabled())
1940 return;
1941
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001942#ifdef CONFIG_HOTPLUG_CPU
1943 for_each_present_cpu(cpu) {
1944 if (cpu_online(cpu))
1945 continue;
1946 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1947 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1948 }
1949#endif
1950
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001951 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001953 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 return;
1955}
1956EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1957
1958
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959/*********************************************************************
1960 * POLICY INTERFACE *
1961 *********************************************************************/
1962
1963/**
1964 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05001965 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1966 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 *
1968 * Reads the current cpufreq policy.
1969 */
1970int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1971{
1972 struct cpufreq_policy *cpu_policy;
1973 if (!policy)
1974 return -EINVAL;
1975
1976 cpu_policy = cpufreq_cpu_get(cpu);
1977 if (!cpu_policy)
1978 return -EINVAL;
1979
Viresh Kumard5b73cd2013-08-06 22:53:06 +05301980 memcpy(policy, cpu_policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
1982 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 return 0;
1984}
1985EXPORT_SYMBOL(cpufreq_get_policy);
1986
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001987/*
Viresh Kumar037ce832013-10-02 14:13:16 +05301988 * policy : current policy.
1989 * new_policy: policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001990 */
Viresh Kumar037ce832013-10-02 14:13:16 +05301991static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301992 struct cpufreq_policy *new_policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993{
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001994 int ret = 0, failed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301996 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", new_policy->cpu,
1997 new_policy->min, new_policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Viresh Kumard5b73cd2013-08-06 22:53:06 +05301999 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302001 if (new_policy->min > policy->max || new_policy->max < policy->min) {
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02002002 ret = -EINVAL;
2003 goto error_out;
2004 }
2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 /* verify the cpu speed can be set within this limit */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302007 ret = cpufreq_driver->verify(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 if (ret)
2009 goto error_out;
2010
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08002012 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302013 CPUFREQ_ADJUST, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
2015 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08002016 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302017 CPUFREQ_INCOMPATIBLE, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Viresh Kumarbb176f72013-06-19 14:19:33 +05302019 /*
2020 * verify the cpu speed can be set within this limit, which might be
2021 * different to the first one
2022 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302023 ret = cpufreq_driver->verify(new_policy);
Alan Sterne041c682006-03-27 01:16:30 -08002024 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
2027 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08002028 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302029 CPUFREQ_NOTIFY, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302031 policy->min = new_policy->min;
2032 policy->max = new_policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002034 pr_debug("new min and max freqs are %u - %u kHz\n",
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302035 policy->min, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002037 if (cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302038 policy->policy = new_policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002039 pr_debug("setting range\n");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302040 ret = cpufreq_driver->setpolicy(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 } else {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302042 if (new_policy->governor != policy->governor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 /* save old, working values */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302044 struct cpufreq_governor *old_gov = policy->governor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002046 pr_debug("governor switch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
2048 /* end old governor */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302049 if (policy->governor) {
2050 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
viresh kumarad7722d2013-10-18 19:10:15 +05302051 up_write(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302052 __cpufreq_governor(policy,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002053 CPUFREQ_GOV_POLICY_EXIT);
viresh kumarad7722d2013-10-18 19:10:15 +05302054 down_write(&policy->rwsem);
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
2057 /* start new governor */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302058 policy->governor = new_policy->governor;
2059 if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
2060 if (!__cpufreq_governor(policy, CPUFREQ_GOV_START)) {
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002061 failed = 0;
Viresh Kumar955ef482013-05-16 05:09:58 +00002062 } else {
viresh kumarad7722d2013-10-18 19:10:15 +05302063 up_write(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302064 __cpufreq_governor(policy,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002065 CPUFREQ_GOV_POLICY_EXIT);
viresh kumarad7722d2013-10-18 19:10:15 +05302066 down_write(&policy->rwsem);
Viresh Kumar955ef482013-05-16 05:09:58 +00002067 }
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002068 }
2069
2070 if (failed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 /* new governor failed, so re-start old one */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002072 pr_debug("starting governor %s failed\n",
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302073 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 if (old_gov) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302075 policy->governor = old_gov;
2076 __cpufreq_governor(policy,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002077 CPUFREQ_GOV_POLICY_INIT);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302078 __cpufreq_governor(policy,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05302079 CPUFREQ_GOV_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 }
2081 ret = -EINVAL;
2082 goto error_out;
2083 }
2084 /* might be a policy change, too, so fall through */
2085 }
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002086 pr_debug("governor: change or update limits\n");
Viresh Kumar3de9bde2013-08-06 22:53:13 +05302087 ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 }
2089
Dave Jones7d5e3502006-02-02 17:03:42 -05002090error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 return ret;
2092}
2093
2094/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
2096 * @cpu: CPU which shall be re-evaluated
2097 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002098 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 * at different times.
2100 */
2101int cpufreq_update_policy(unsigned int cpu)
2102{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302103 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2104 struct cpufreq_policy new_policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02002105 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302107 if (!policy) {
Julia Lawallf1829e42008-07-25 22:44:53 +02002108 ret = -ENODEV;
2109 goto no_policy;
2110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
viresh kumarad7722d2013-10-18 19:10:15 +05302112 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002114 pr_debug("updating policy for CPU %u\n", cpu);
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302115 memcpy(&new_policy, policy, sizeof(*policy));
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302116 new_policy.min = policy->user_policy.min;
2117 new_policy.max = policy->user_policy.max;
2118 new_policy.policy = policy->user_policy.policy;
2119 new_policy.governor = policy->user_policy.governor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Viresh Kumarbb176f72013-06-19 14:19:33 +05302121 /*
2122 * BIOS might change freq behind our back
2123 * -> ask driver for current freq and notify governors about a change
2124 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002125 if (cpufreq_driver->get) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302126 new_policy.cur = cpufreq_driver->get(cpu);
2127 if (!policy->cur) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002128 pr_debug("Driver did not initialize current freq");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302129 policy->cur = new_policy.cur;
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002130 } else {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302131 if (policy->cur != new_policy.cur && has_target())
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302132 cpufreq_out_of_sync(cpu, policy->cur,
2133 new_policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002134 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01002135 }
2136
Viresh Kumar037ce832013-10-02 14:13:16 +05302137 ret = cpufreq_set_policy(policy, &new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
viresh kumarad7722d2013-10-18 19:10:15 +05302139 up_write(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002140
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302141 cpufreq_cpu_put(policy);
Julia Lawallf1829e42008-07-25 22:44:53 +02002142no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 return ret;
2144}
2145EXPORT_SYMBOL(cpufreq_update_policy);
2146
Paul Gortmaker27609842013-06-19 13:54:04 -04002147static int cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002148 unsigned long action, void *hcpu)
2149{
2150 unsigned int cpu = (unsigned long)hcpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002151 struct device *dev;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302152 bool frozen = false;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002153
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002154 dev = get_cpu_device(cpu);
2155 if (dev) {
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302156
Rafael J. Wysockid4faadd2013-12-08 01:23:58 +01002157 if (action & CPU_TASKS_FROZEN)
2158 frozen = true;
2159
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302160 switch (action & ~CPU_TASKS_FROZEN) {
Ashok Rajc32b6b82005-10-30 14:59:54 -08002161 case CPU_ONLINE:
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302162 __cpufreq_add_dev(dev, NULL, frozen);
Srivatsa S. Bhat23d328992013-07-30 04:23:56 +05302163 cpufreq_update_policy(cpu);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002164 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302165
Ashok Rajc32b6b82005-10-30 14:59:54 -08002166 case CPU_DOWN_PREPARE:
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05302167 __cpufreq_remove_dev_prepare(dev, NULL, frozen);
Srivatsa S. Bhat1aee40a2013-09-07 01:23:27 +05302168 break;
2169
2170 case CPU_POST_DEAD:
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05302171 __cpufreq_remove_dev_finish(dev, NULL, frozen);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002172 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302173
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002174 case CPU_DOWN_FAILED:
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302175 __cpufreq_add_dev(dev, NULL, frozen);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002176 break;
2177 }
2178 }
2179 return NOTIFY_OK;
2180}
2181
Neal Buckendahl9c36f742010-06-22 22:02:44 -05002182static struct notifier_block __refdata cpufreq_cpu_notifier = {
Viresh Kumarbb176f72013-06-19 14:19:33 +05302183 .notifier_call = cpufreq_cpu_callback,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002184};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
2186/*********************************************************************
2187 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2188 *********************************************************************/
2189
2190/**
2191 * cpufreq_register_driver - register a CPU Frequency driver
2192 * @driver_data: A struct cpufreq_driver containing the values#
2193 * submitted by the CPU Frequency driver.
2194 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302195 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05002197 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 *
2199 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002200int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
2202 unsigned long flags;
2203 int ret;
2204
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002205 if (cpufreq_disabled())
2206 return -ENODEV;
2207
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 if (!driver_data || !driver_data->verify || !driver_data->init ||
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302209 !(driver_data->setpolicy || driver_data->target_index ||
2210 driver_data->target))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 return -EINVAL;
2212
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002213 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
2215 if (driver_data->setpolicy)
2216 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2217
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002218 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002219 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002220 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Yinghai Lu4dea58062013-09-18 21:05:20 -07002221 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002223 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002224 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002226 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002227 if (ret)
2228 goto err_null_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002230 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 int i;
2232 ret = -ENODEV;
2233
2234 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07002235 for (i = 0; i < nr_cpu_ids; i++)
2236 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07002238 break;
2239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
2241 /* if all ->init() calls failed, unregister */
2242 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002243 pr_debug("no CPU initialized for driver %s\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05302244 driver_data->name);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002245 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 }
2247 }
2248
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002249 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002250 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002252 return 0;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002253err_if_unreg:
2254 subsys_interface_unregister(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002255err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002256 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002257 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002258 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05002259 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260}
2261EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2262
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263/**
2264 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2265 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302266 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 * the right to do so, i.e. if you have succeeded in initialising before!
2268 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2269 * currently not initialised.
2270 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002271int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272{
2273 unsigned long flags;
2274
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002275 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002278 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002280 subsys_interface_unregister(&cpufreq_interface);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07002281 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
Viresh Kumar6eed9402013-08-06 22:53:11 +05302283 down_write(&cpufreq_rwsem);
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002284 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302285
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002286 cpufreq_driver = NULL;
Viresh Kumar6eed9402013-08-06 22:53:11 +05302287
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002288 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302289 up_write(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
2291 return 0;
2292}
2293EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002294
2295static int __init cpufreq_core_init(void)
2296{
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002297 if (cpufreq_disabled())
2298 return -ENODEV;
2299
Viresh Kumar2361be22013-05-17 16:09:09 +05302300 cpufreq_global_kobject = kobject_create();
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002301 BUG_ON(!cpufreq_global_kobject);
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01002302 register_syscore_ops(&cpufreq_syscore_ops);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002303
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002304 return 0;
2305}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002306core_initcall(cpufreq_core_init);