blob: 2de2f1ddd95f74ad30e4ab06e67dafbe16f9c4e7 [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/* This one keeps track of the previously set governor of a removed CPU */
Dmitry Monakhove77b89f2009-10-05 00:38:55 +040046static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +053048static inline bool has_target(void)
49{
50 return cpufreq_driver->target_index || cpufreq_driver->target;
51}
52
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080053/*
Viresh Kumar6eed9402013-08-06 22:53:11 +053054 * rwsem to guarantee that cpufreq driver module doesn't unload during critical
55 * sections
56 */
57static DECLARE_RWSEM(cpufreq_rwsem);
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/* internal prototypes */
Dave Jones29464f22009-01-18 01:37:11 -050060static int __cpufreq_governor(struct cpufreq_policy *policy,
61 unsigned int event);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080062static unsigned int __cpufreq_get(unsigned int cpu);
David Howells65f27f32006-11-22 14:55:48 +000063static void handle_update(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/**
Dave Jones32ee8c32006-02-28 00:43:23 -050066 * Two notifier lists: the "policy" list is involved in the
67 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 * "transition" list for kernel code that needs to handle
69 * changes to devices when the CPU clock speed changes.
70 * The mutex locks both lists.
71 */
Alan Sterne041c682006-03-27 01:16:30 -080072static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -070073static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -020075static bool init_cpufreq_transition_notifier_list_called;
Alan Sternb4dfdbb2006-10-04 02:17:06 -070076static int __init init_cpufreq_transition_notifier_list(void)
77{
78 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -020079 init_cpufreq_transition_notifier_list_called = true;
Alan Sternb4dfdbb2006-10-04 02:17:06 -070080 return 0;
81}
Linus Torvaldsb3438f82006-11-20 11:47:18 -080082pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -040084static int off __read_mostly;
Viresh Kumarda584452012-10-26 00:51:32 +020085static int cpufreq_disabled(void)
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -040086{
87 return off;
88}
89void disable_cpufreq(void)
90{
91 off = 1;
92}
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static LIST_HEAD(cpufreq_governor_list);
Dave Jones29464f22009-01-18 01:37:11 -050094static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000096bool have_governor_per_policy(void)
97{
Viresh Kumar0b981e72013-10-02 14:13:18 +053098 return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000099}
Viresh Kumar3f869d62013-05-16 05:09:56 +0000100EXPORT_SYMBOL_GPL(have_governor_per_policy);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000101
Viresh Kumar944e9a02013-05-16 05:09:57 +0000102struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
103{
104 if (have_governor_per_policy())
105 return &policy->kobj;
106 else
107 return cpufreq_global_kobject;
108}
109EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
110
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000111static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
112{
113 u64 idle_time;
114 u64 cur_wall_time;
115 u64 busy_time;
116
117 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
118
119 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
120 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
121 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
122 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
123 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
124 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
125
126 idle_time = cur_wall_time - busy_time;
127 if (wall)
128 *wall = cputime_to_usecs(cur_wall_time);
129
130 return cputime_to_usecs(idle_time);
131}
132
133u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
134{
135 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
136
137 if (idle_time == -1ULL)
138 return get_cpu_idle_time_jiffy(cpu, wall);
139 else if (!io_busy)
140 idle_time += get_cpu_iowait_time_us(cpu, wall);
141
142 return idle_time;
143}
144EXPORT_SYMBOL_GPL(get_cpu_idle_time);
145
Viresh Kumar70e9e772013-10-03 20:29:07 +0530146/*
147 * This is a generic cpufreq init() routine which can be used by cpufreq
148 * drivers of SMP systems. It will do following:
149 * - validate & show freq table passed
150 * - set policies transition latency
151 * - policy->cpus with all possible CPUs
152 */
153int cpufreq_generic_init(struct cpufreq_policy *policy,
154 struct cpufreq_frequency_table *table,
155 unsigned int transition_latency)
156{
157 int ret;
158
159 ret = cpufreq_table_validate_and_show(policy, table);
160 if (ret) {
161 pr_err("%s: invalid frequency table: %d\n", __func__, ret);
162 return ret;
163 }
164
165 policy->cpuinfo.transition_latency = transition_latency;
166
167 /*
168 * The driver only supports the SMP configuartion where all processors
169 * share the clock and voltage and clock.
170 */
171 cpumask_setall(policy->cpus);
172
173 return 0;
174}
175EXPORT_SYMBOL_GPL(cpufreq_generic_init);
176
Viresh Kumar652ed952014-01-09 20:38:43 +0530177unsigned int cpufreq_generic_get(unsigned int cpu)
178{
179 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
180
181 if (!policy || IS_ERR(policy->clk)) {
182 pr_err("%s: No %s associated to cpu: %d\n", __func__,
183 policy ? "clk" : "policy", cpu);
184 return 0;
185 }
186
187 return clk_get_rate(policy->clk) / 1000;
188}
189EXPORT_SYMBOL_GPL(cpufreq_generic_get);
190
Viresh Kumar6eed9402013-08-06 22:53:11 +0530191struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530193 struct cpufreq_policy *policy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 unsigned long flags;
195
Viresh Kumar6eed9402013-08-06 22:53:11 +0530196 if (cpufreq_disabled() || (cpu >= nr_cpu_ids))
197 return NULL;
198
199 if (!down_read_trylock(&cpufreq_rwsem))
200 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 /* get the cpufreq driver */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000203 read_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Viresh Kumar6eed9402013-08-06 22:53:11 +0530205 if (cpufreq_driver) {
206 /* get the CPU */
207 policy = per_cpu(cpufreq_cpu_data, cpu);
208 if (policy)
209 kobject_get(&policy->kobj);
210 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200211
Viresh Kumar6eed9402013-08-06 22:53:11 +0530212 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530214 if (!policy)
Viresh Kumar6eed9402013-08-06 22:53:11 +0530215 up_read(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530217 return policy;
Stephen Boyda9144432012-07-20 18:14:38 +0000218}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
220
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530221void cpufreq_cpu_put(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000223 if (cpufreq_disabled())
224 return;
225
Viresh Kumar6eed9402013-08-06 22:53:11 +0530226 kobject_put(&policy->kobj);
227 up_read(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
233 *********************************************************************/
234
235/**
236 * adjust_jiffies - adjust the system "loops_per_jiffy"
237 *
238 * This function alters the system "loops_per_jiffy" for the clock
239 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500240 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 * per-CPU loops_per_jiffy value wherever possible.
242 */
243#ifndef CONFIG_SMP
244static unsigned long l_p_j_ref;
Viresh Kumarbb176f72013-06-19 14:19:33 +0530245static unsigned int l_p_j_ref_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Arjan van de Ven858119e2006-01-14 13:20:43 -0800247static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 if (ci->flags & CPUFREQ_CONST_LOOPS)
250 return;
251
252 if (!l_p_j_ref_freq) {
253 l_p_j_ref = loops_per_jiffy;
254 l_p_j_ref_freq = ci->old;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200255 pr_debug("saving %lu as reference value for loops_per_jiffy; "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530256 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
Viresh Kumarbb176f72013-06-19 14:19:33 +0530258 if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700259 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530260 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
261 ci->new);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200262 pr_debug("scaling loops_per_jiffy to %lu "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530263 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265}
266#else
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530267static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
268{
269 return;
270}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271#endif
272
Viresh Kumar0956df9c2013-06-19 14:19:34 +0530273static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530274 struct cpufreq_freqs *freqs, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 BUG_ON(irqs_disabled());
277
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000278 if (cpufreq_disabled())
279 return;
280
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200281 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200282 pr_debug("notification %u of frequency transition to %u kHz\n",
Dave Jonese4472cb2006-01-31 15:53:55 -0800283 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500288 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800289 * which is not equal to what the cpufreq core thinks is
290 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200292 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800293 if ((policy) && (policy->cpu == freqs->cpu) &&
294 (policy->cur) && (policy->cur != freqs->old)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200295 pr_debug("Warning: CPU frequency is"
Dave Jonese4472cb2006-01-31 15:53:55 -0800296 " %u, cpufreq assumed %u kHz.\n",
297 freqs->old, policy->cur);
298 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700301 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800302 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
304 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 case CPUFREQ_POSTCHANGE:
307 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200308 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
Thomas Renninger6f4f2722010-04-20 13:17:36 +0200309 (unsigned long)freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100310 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700311 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800312 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800313 if (likely(policy) && likely(policy->cpu == freqs->cpu))
314 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 break;
316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
Viresh Kumarbb176f72013-06-19 14:19:33 +0530318
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530319/**
320 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
321 * on frequency transition.
322 *
323 * This function calls the transition notifiers and the "adjust_jiffies"
324 * function. It is called twice on all CPU frequency changes that have
325 * external effects.
326 */
327void cpufreq_notify_transition(struct cpufreq_policy *policy,
328 struct cpufreq_freqs *freqs, unsigned int state)
329{
330 for_each_cpu(freqs->cpu, policy->cpus)
331 __cpufreq_notify_transition(policy, freqs, state);
332}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
334
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530335/* Do post notifications when there are chances that transition has failed */
336void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
337 struct cpufreq_freqs *freqs, int transition_failed)
338{
339 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
340 if (!transition_failed)
341 return;
342
343 swap(freqs->old, freqs->new);
344 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
345 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
346}
347EXPORT_SYMBOL_GPL(cpufreq_notify_post_transition);
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350/*********************************************************************
351 * SYSFS INTERFACE *
352 *********************************************************************/
Rashika Kheria8a5c74a2014-02-26 22:12:42 +0530353static ssize_t show_boost(struct kobject *kobj,
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100354 struct attribute *attr, char *buf)
355{
356 return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
357}
358
359static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
360 const char *buf, size_t count)
361{
362 int ret, enable;
363
364 ret = sscanf(buf, "%d", &enable);
365 if (ret != 1 || enable < 0 || enable > 1)
366 return -EINVAL;
367
368 if (cpufreq_boost_trigger_state(enable)) {
369 pr_err("%s: Cannot %s BOOST!\n", __func__,
370 enable ? "enable" : "disable");
371 return -EINVAL;
372 }
373
374 pr_debug("%s: cpufreq BOOST %s\n", __func__,
375 enable ? "enabled" : "disabled");
376
377 return count;
378}
379define_one_global_rw(boost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700381static struct cpufreq_governor *__find_governor(const char *str_governor)
382{
383 struct cpufreq_governor *t;
384
385 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
Dave Jones29464f22009-01-18 01:37:11 -0500386 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700387 return t;
388
389 return NULL;
390}
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392/**
393 * cpufreq_parse_governor - parse a governor string
394 */
Dave Jones905d77c2008-03-05 14:28:32 -0500395static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 struct cpufreq_governor **governor)
397{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700398 int err = -EINVAL;
399
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200400 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700401 goto out;
402
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200403 if (cpufreq_driver->setpolicy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
405 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700406 err = 0;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530407 } else if (!strnicmp(str_governor, "powersave",
408 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700410 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530412 } else if (has_target()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700414
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800415 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700416
417 t = __find_governor(str_governor);
418
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700419 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700420 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700421
Kees Cook1a8e1462011-05-04 08:38:56 -0700422 mutex_unlock(&cpufreq_governor_mutex);
423 ret = request_module("cpufreq_%s", str_governor);
424 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700425
Kees Cook1a8e1462011-05-04 08:38:56 -0700426 if (ret == 0)
427 t = __find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700428 }
429
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700430 if (t != NULL) {
431 *governor = t;
432 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700434
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800435 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
Dave Jones29464f22009-01-18 01:37:11 -0500437out:
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700438 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530442 * cpufreq_per_cpu_attr_read() / show_##file_name() -
443 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 *
445 * Write out information from cpufreq_driver->policy[cpu]; object must be
446 * "unsigned int".
447 */
448
Dave Jones32ee8c32006-02-28 00:43:23 -0500449#define show_one(file_name, object) \
450static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500451(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500452{ \
Dave Jones29464f22009-01-18 01:37:11 -0500453 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
456show_one(cpuinfo_min_freq, cpuinfo.min_freq);
457show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100458show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459show_one(scaling_min_freq, min);
460show_one(scaling_max_freq, max);
461show_one(scaling_cur_freq, cur);
462
Viresh Kumar037ce832013-10-02 14:13:16 +0530463static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530464 struct cpufreq_policy *new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466/**
467 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
468 */
469#define store_one(file_name, object) \
470static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500471(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{ \
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530473 int ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 struct cpufreq_policy new_policy; \
475 \
476 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
477 if (ret) \
478 return -EINVAL; \
479 \
Dave Jones29464f22009-01-18 01:37:11 -0500480 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (ret != 1) \
482 return -EINVAL; \
483 \
Viresh Kumar037ce832013-10-02 14:13:16 +0530484 ret = cpufreq_set_policy(policy, &new_policy); \
Thomas Renninger7970e082006-04-13 15:14:04 +0200485 policy->user_policy.object = policy->object; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 \
487 return ret ? ret : count; \
488}
489
Dave Jones29464f22009-01-18 01:37:11 -0500490store_one(scaling_min_freq, min);
491store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493/**
494 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
495 */
Dave Jones905d77c2008-03-05 14:28:32 -0500496static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
497 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800499 unsigned int cur_freq = __cpufreq_get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (!cur_freq)
501 return sprintf(buf, "<unknown>");
502 return sprintf(buf, "%u\n", cur_freq);
503}
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505/**
506 * show_scaling_governor - show the current policy for the specified CPU
507 */
Dave Jones905d77c2008-03-05 14:28:32 -0500508static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Dave Jones29464f22009-01-18 01:37:11 -0500510 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return sprintf(buf, "powersave\n");
512 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
513 return sprintf(buf, "performance\n");
514 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200515 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500516 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return -EINVAL;
518}
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520/**
521 * store_scaling_governor - store policy for the specified CPU
522 */
Dave Jones905d77c2008-03-05 14:28:32 -0500523static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
524 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530526 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 char str_governor[16];
528 struct cpufreq_policy new_policy;
529
530 ret = cpufreq_get_policy(&new_policy, policy->cpu);
531 if (ret)
532 return ret;
533
Dave Jones29464f22009-01-18 01:37:11 -0500534 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (ret != 1)
536 return -EINVAL;
537
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530538 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
539 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return -EINVAL;
541
Viresh Kumar037ce832013-10-02 14:13:16 +0530542 ret = cpufreq_set_policy(policy, &new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200543
544 policy->user_policy.policy = policy->policy;
545 policy->user_policy.governor = policy->governor;
Thomas Renninger7970e082006-04-13 15:14:04 +0200546
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530547 if (ret)
548 return ret;
549 else
550 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
553/**
554 * show_scaling_driver - show the cpufreq driver currently loaded
555 */
Dave Jones905d77c2008-03-05 14:28:32 -0500556static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200558 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
561/**
562 * show_scaling_available_governors - show the available CPUfreq governors
563 */
Dave Jones905d77c2008-03-05 14:28:32 -0500564static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
565 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
567 ssize_t i = 0;
568 struct cpufreq_governor *t;
569
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530570 if (!has_target()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 i += sprintf(buf, "performance powersave");
572 goto out;
573 }
574
575 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
Dave Jones29464f22009-01-18 01:37:11 -0500576 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
577 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200579 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500581out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 i += sprintf(&buf[i], "\n");
583 return i;
584}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700585
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800586ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
588 ssize_t i = 0;
589 unsigned int cpu;
590
Rusty Russell835481d2009-01-04 05:18:06 -0800591 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 if (i)
593 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
594 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
595 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500596 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598 i += sprintf(&buf[i], "\n");
599 return i;
600}
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800601EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700603/**
604 * show_related_cpus - show the CPUs affected by each transition even if
605 * hw coordination is in use
606 */
607static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
608{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800609 return cpufreq_show_cpus(policy->related_cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700610}
611
612/**
613 * show_affected_cpus - show the CPUs affected by each transition
614 */
615static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
616{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800617 return cpufreq_show_cpus(policy->cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700618}
619
Venki Pallipadi9e769882007-10-26 10:18:21 -0700620static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500621 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700622{
623 unsigned int freq = 0;
624 unsigned int ret;
625
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700626 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700627 return -EINVAL;
628
629 ret = sscanf(buf, "%u", &freq);
630 if (ret != 1)
631 return -EINVAL;
632
633 policy->governor->store_setspeed(policy, freq);
634
635 return count;
636}
637
638static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
639{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700640 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700641 return sprintf(buf, "<unsupported>\n");
642
643 return policy->governor->show_setspeed(policy, buf);
644}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Thomas Renningere2f74f32009-11-19 12:31:01 +0100646/**
viresh kumar8bf1ac722012-10-23 01:23:33 +0200647 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100648 */
649static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
650{
651 unsigned int limit;
652 int ret;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200653 if (cpufreq_driver->bios_limit) {
654 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
Thomas Renningere2f74f32009-11-19 12:31:01 +0100655 if (!ret)
656 return sprintf(buf, "%u\n", limit);
657 }
658 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
659}
660
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200661cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
662cpufreq_freq_attr_ro(cpuinfo_min_freq);
663cpufreq_freq_attr_ro(cpuinfo_max_freq);
664cpufreq_freq_attr_ro(cpuinfo_transition_latency);
665cpufreq_freq_attr_ro(scaling_available_governors);
666cpufreq_freq_attr_ro(scaling_driver);
667cpufreq_freq_attr_ro(scaling_cur_freq);
668cpufreq_freq_attr_ro(bios_limit);
669cpufreq_freq_attr_ro(related_cpus);
670cpufreq_freq_attr_ro(affected_cpus);
671cpufreq_freq_attr_rw(scaling_min_freq);
672cpufreq_freq_attr_rw(scaling_max_freq);
673cpufreq_freq_attr_rw(scaling_governor);
674cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Dave Jones905d77c2008-03-05 14:28:32 -0500676static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 &cpuinfo_min_freq.attr,
678 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100679 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 &scaling_min_freq.attr,
681 &scaling_max_freq.attr,
682 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700683 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 &scaling_governor.attr,
685 &scaling_driver.attr,
686 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700687 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 NULL
689};
690
Dave Jones29464f22009-01-18 01:37:11 -0500691#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
692#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Dave Jones29464f22009-01-18 01:37:11 -0500694static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Dave Jones905d77c2008-03-05 14:28:32 -0500696 struct cpufreq_policy *policy = to_policy(kobj);
697 struct freq_attr *fattr = to_attr(attr);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530698 ssize_t ret;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530699
700 if (!down_read_trylock(&cpufreq_rwsem))
Viresh Kumar1b750e32013-10-02 14:13:09 +0530701 return -EINVAL;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800702
viresh kumarad7722d2013-10-18 19:10:15 +0530703 down_read(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800704
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530705 if (fattr->show)
706 ret = fattr->show(policy, buf);
707 else
708 ret = -EIO;
709
viresh kumarad7722d2013-10-18 19:10:15 +0530710 up_read(&policy->rwsem);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530711 up_read(&cpufreq_rwsem);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return ret;
714}
715
Dave Jones905d77c2008-03-05 14:28:32 -0500716static ssize_t store(struct kobject *kobj, struct attribute *attr,
717 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
Dave Jones905d77c2008-03-05 14:28:32 -0500719 struct cpufreq_policy *policy = to_policy(kobj);
720 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500721 ssize_t ret = -EINVAL;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530722
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530723 get_online_cpus();
724
725 if (!cpu_online(policy->cpu))
726 goto unlock;
727
Viresh Kumar6eed9402013-08-06 22:53:11 +0530728 if (!down_read_trylock(&cpufreq_rwsem))
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530729 goto unlock;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800730
viresh kumarad7722d2013-10-18 19:10:15 +0530731 down_write(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800732
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530733 if (fattr->store)
734 ret = fattr->store(policy, buf, count);
735 else
736 ret = -EIO;
737
viresh kumarad7722d2013-10-18 19:10:15 +0530738 up_write(&policy->rwsem);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530739
Viresh Kumar6eed9402013-08-06 22:53:11 +0530740 up_read(&cpufreq_rwsem);
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530741unlock:
742 put_online_cpus();
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return ret;
745}
746
Dave Jones905d77c2008-03-05 14:28:32 -0500747static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Dave Jones905d77c2008-03-05 14:28:32 -0500749 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200750 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 complete(&policy->kobj_unregister);
752}
753
Emese Revfy52cf25d2010-01-19 02:58:23 +0100754static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 .show = show,
756 .store = store,
757};
758
759static struct kobj_type ktype_cpufreq = {
760 .sysfs_ops = &sysfs_ops,
761 .default_attrs = default_attrs,
762 .release = cpufreq_sysfs_release,
763};
764
Viresh Kumar2361be22013-05-17 16:09:09 +0530765struct kobject *cpufreq_global_kobject;
766EXPORT_SYMBOL(cpufreq_global_kobject);
767
768static int cpufreq_global_kobject_usage;
769
770int cpufreq_get_global_kobject(void)
771{
772 if (!cpufreq_global_kobject_usage++)
773 return kobject_add(cpufreq_global_kobject,
774 &cpu_subsys.dev_root->kobj, "%s", "cpufreq");
775
776 return 0;
777}
778EXPORT_SYMBOL(cpufreq_get_global_kobject);
779
780void cpufreq_put_global_kobject(void)
781{
782 if (!--cpufreq_global_kobject_usage)
783 kobject_del(cpufreq_global_kobject);
784}
785EXPORT_SYMBOL(cpufreq_put_global_kobject);
786
787int cpufreq_sysfs_create_file(const struct attribute *attr)
788{
789 int ret = cpufreq_get_global_kobject();
790
791 if (!ret) {
792 ret = sysfs_create_file(cpufreq_global_kobject, attr);
793 if (ret)
794 cpufreq_put_global_kobject();
795 }
796
797 return ret;
798}
799EXPORT_SYMBOL(cpufreq_sysfs_create_file);
800
801void cpufreq_sysfs_remove_file(const struct attribute *attr)
802{
803 sysfs_remove_file(cpufreq_global_kobject, attr);
804 cpufreq_put_global_kobject();
805}
806EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
807
Dave Jones19d6f7e2009-07-08 17:35:39 -0400808/* symlink affected CPUs */
Viresh Kumar308b60e2013-07-31 14:35:14 +0200809static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400810{
811 unsigned int j;
812 int ret = 0;
813
814 for_each_cpu(j, policy->cpus) {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800815 struct device *cpu_dev;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400816
Viresh Kumar308b60e2013-07-31 14:35:14 +0200817 if (j == policy->cpu)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400818 continue;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400819
Viresh Kumare8fdde12013-07-31 14:31:33 +0200820 pr_debug("Adding link for CPU: %u\n", j);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800821 cpu_dev = get_cpu_device(j);
822 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Dave Jones19d6f7e2009-07-08 17:35:39 -0400823 "cpufreq");
Rafael J. Wysocki71c34612013-08-04 01:19:34 +0200824 if (ret)
825 break;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400826 }
827 return ret;
828}
829
Viresh Kumar308b60e2013-07-31 14:35:14 +0200830static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800831 struct device *dev)
Dave Jones909a6942009-07-08 18:05:42 -0400832{
833 struct freq_attr **drv_attr;
Dave Jones909a6942009-07-08 18:05:42 -0400834 int ret = 0;
Dave Jones909a6942009-07-08 18:05:42 -0400835
836 /* prepare interface data */
837 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800838 &dev->kobj, "cpufreq");
Dave Jones909a6942009-07-08 18:05:42 -0400839 if (ret)
840 return ret;
841
842 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200843 drv_attr = cpufreq_driver->attr;
Dave Jones909a6942009-07-08 18:05:42 -0400844 while ((drv_attr) && (*drv_attr)) {
845 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
846 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200847 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400848 drv_attr++;
849 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200850 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -0400851 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
852 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200853 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400854 }
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530855 if (has_target()) {
Dave Jones909a6942009-07-08 18:05:42 -0400856 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
857 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200858 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400859 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200860 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +0100861 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
862 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200863 goto err_out_kobj_put;
Thomas Renningere2f74f32009-11-19 12:31:01 +0100864 }
Dave Jones909a6942009-07-08 18:05:42 -0400865
Viresh Kumar308b60e2013-07-31 14:35:14 +0200866 ret = cpufreq_add_dev_symlink(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400867 if (ret)
868 goto err_out_kobj_put;
869
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530870 return ret;
871
872err_out_kobj_put:
873 kobject_put(&policy->kobj);
874 wait_for_completion(&policy->kobj_unregister);
875 return ret;
876}
877
878static void cpufreq_init_policy(struct cpufreq_policy *policy)
879{
viresh kumar6e2c89d2014-03-04 11:43:59 +0800880 struct cpufreq_governor *gov = NULL;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530881 struct cpufreq_policy new_policy;
882 int ret = 0;
883
Viresh Kumard5b73cd2013-08-06 22:53:06 +0530884 memcpy(&new_policy, policy, sizeof(*policy));
Jason Barona27a9ab2013-12-19 22:50:50 +0000885
viresh kumar6e2c89d2014-03-04 11:43:59 +0800886 /* Update governor of new_policy to the governor used before hotplug */
887 gov = __find_governor(per_cpu(cpufreq_cpu_governor, policy->cpu));
888 if (gov)
889 pr_debug("Restoring governor %s for cpu %d\n",
890 policy->governor->name, policy->cpu);
891 else
892 gov = CPUFREQ_DEFAULT_GOVERNOR;
893
894 new_policy.governor = gov;
895
Jason Barona27a9ab2013-12-19 22:50:50 +0000896 /* Use the default policy if its valid. */
897 if (cpufreq_driver->setpolicy)
viresh kumar6e2c89d2014-03-04 11:43:59 +0800898 cpufreq_parse_governor(gov->name, &new_policy.policy, NULL);
Dave Jonesecf7e462009-07-08 18:48:47 -0400899
900 /* set default policy */
Viresh Kumar037ce832013-10-02 14:13:16 +0530901 ret = cpufreq_set_policy(policy, &new_policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400902 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200903 pr_debug("setting policy failed\n");
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200904 if (cpufreq_driver->exit)
905 cpufreq_driver->exit(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400906 }
Dave Jones909a6942009-07-08 18:05:42 -0400907}
908
Viresh Kumarfcf80582013-01-29 14:39:08 +0000909#ifdef CONFIG_HOTPLUG_CPU
Viresh Kumard8d3b472013-08-04 01:20:07 +0200910static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,
Viresh Kumar42f921a2013-12-20 21:26:02 +0530911 unsigned int cpu, struct device *dev)
Viresh Kumarfcf80582013-01-29 14:39:08 +0000912{
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530913 int ret = 0;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000914 unsigned long flags;
915
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530916 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +0530917 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
918 if (ret) {
919 pr_err("%s: Failed to stop governor\n", __func__);
920 return ret;
921 }
922 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000923
viresh kumarad7722d2013-10-18 19:10:15 +0530924 down_write(&policy->rwsem);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530925
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000926 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530927
Viresh Kumarfcf80582013-01-29 14:39:08 +0000928 cpumask_set_cpu(cpu, policy->cpus);
929 per_cpu(cpufreq_cpu_data, cpu) = policy;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000930 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000931
viresh kumarad7722d2013-10-18 19:10:15 +0530932 up_write(&policy->rwsem);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530933
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530934 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +0530935 if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
936 (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
937 pr_err("%s: Failed to start governor\n", __func__);
938 return ret;
939 }
Viresh Kumar820c6ca2013-04-22 00:48:03 +0200940 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000941
Viresh Kumar42f921a2013-12-20 21:26:02 +0530942 return sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
Viresh Kumarfcf80582013-01-29 14:39:08 +0000943}
944#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530946static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu)
947{
948 struct cpufreq_policy *policy;
949 unsigned long flags;
950
Lan Tianyu44871c92013-09-11 15:05:05 +0800951 read_lock_irqsave(&cpufreq_driver_lock, flags);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530952
953 policy = per_cpu(cpufreq_cpu_data_fallback, cpu);
954
Lan Tianyu44871c92013-09-11 15:05:05 +0800955 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530956
viresh kumar6e2c89d2014-03-04 11:43:59 +0800957 policy->governor = NULL;
958
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530959 return policy;
960}
961
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +0530962static struct cpufreq_policy *cpufreq_policy_alloc(void)
963{
964 struct cpufreq_policy *policy;
965
966 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
967 if (!policy)
968 return NULL;
969
970 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
971 goto err_free_policy;
972
973 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
974 goto err_free_cpumask;
975
Lukasz Majewskic88a1f82013-08-06 22:53:08 +0530976 INIT_LIST_HEAD(&policy->policy_list);
viresh kumarad7722d2013-10-18 19:10:15 +0530977 init_rwsem(&policy->rwsem);
978
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +0530979 return policy;
980
981err_free_cpumask:
982 free_cpumask_var(policy->cpus);
983err_free_policy:
984 kfree(policy);
985
986 return NULL;
987}
988
Viresh Kumar42f921a2013-12-20 21:26:02 +0530989static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
990{
991 struct kobject *kobj;
992 struct completion *cmp;
993
Viresh Kumarfcd7af92014-01-07 07:10:10 +0530994 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
995 CPUFREQ_REMOVE_POLICY, policy);
996
Viresh Kumar42f921a2013-12-20 21:26:02 +0530997 down_read(&policy->rwsem);
998 kobj = &policy->kobj;
999 cmp = &policy->kobj_unregister;
1000 up_read(&policy->rwsem);
1001 kobject_put(kobj);
1002
1003 /*
1004 * We need to make sure that the underlying kobj is
1005 * actually not referenced anymore by anybody before we
1006 * proceed with unloading.
1007 */
1008 pr_debug("waiting for dropping of refcount\n");
1009 wait_for_completion(cmp);
1010 pr_debug("wait complete\n");
1011}
1012
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301013static void cpufreq_policy_free(struct cpufreq_policy *policy)
1014{
1015 free_cpumask_var(policy->related_cpus);
1016 free_cpumask_var(policy->cpus);
1017 kfree(policy);
1018}
1019
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301020static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
1021{
Srivatsa S. Bhat99ec8992013-09-12 17:29:09 +05301022 if (WARN_ON(cpu == policy->cpu))
Srivatsa S. Bhatcb38ed52013-09-12 01:43:42 +05301023 return;
1024
viresh kumarad7722d2013-10-18 19:10:15 +05301025 down_write(&policy->rwsem);
Viresh Kumar8efd5762013-09-17 10:22:11 +05301026
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301027 policy->last_cpu = policy->cpu;
1028 policy->cpu = cpu;
1029
viresh kumarad7722d2013-10-18 19:10:15 +05301030 up_write(&policy->rwsem);
Viresh Kumar8efd5762013-09-17 10:22:11 +05301031
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301032 cpufreq_frequency_table_update_policy_cpu(policy);
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301033 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1034 CPUFREQ_UPDATE_POLICY_CPU, policy);
1035}
1036
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301037static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif,
1038 bool frozen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
Viresh Kumarfcf80582013-01-29 14:39:08 +00001040 unsigned int j, cpu = dev->id;
Viresh Kumar65922462013-02-07 10:56:03 +05301041 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 unsigned long flags;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001044#ifdef CONFIG_HOTPLUG_CPU
Viresh Kumar1b274292013-08-20 12:08:26 +05301045 struct cpufreq_policy *tpolicy;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001046#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Ashok Rajc32b6b82005-10-30 14:59:54 -08001048 if (cpu_is_offline(cpu))
1049 return 0;
1050
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001051 pr_debug("adding CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053#ifdef CONFIG_SMP
1054 /* check whether a different CPU already registered this
1055 * CPU because it is in the same boat. */
1056 policy = cpufreq_cpu_get(cpu);
1057 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -05001058 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return 0;
1060 }
Li Zhong5025d622013-08-21 01:31:08 +02001061#endif
Viresh Kumarfcf80582013-01-29 14:39:08 +00001062
Viresh Kumar6eed9402013-08-06 22:53:11 +05301063 if (!down_read_trylock(&cpufreq_rwsem))
1064 return 0;
1065
Viresh Kumarfcf80582013-01-29 14:39:08 +00001066#ifdef CONFIG_HOTPLUG_CPU
1067 /* Check if this cpu was hot-unplugged earlier and has siblings */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001068 read_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar1b274292013-08-20 12:08:26 +05301069 list_for_each_entry(tpolicy, &cpufreq_policy_list, policy_list) {
1070 if (cpumask_test_cpu(cpu, tpolicy->related_cpus)) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001071 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301072 ret = cpufreq_add_policy_cpu(tpolicy, cpu, dev);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301073 up_read(&cpufreq_rwsem);
1074 return ret;
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301075 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001076 }
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001077 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +00001078#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001080 /*
1081 * Restore the saved policy when doing light-weight init and fall back
1082 * to the full init if that fails.
1083 */
1084 policy = frozen ? cpufreq_policy_restore(cpu) : NULL;
1085 if (!policy) {
1086 frozen = false;
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301087 policy = cpufreq_policy_alloc();
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001088 if (!policy)
1089 goto nomem_out;
1090 }
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301091
1092 /*
1093 * In the resume path, since we restore a saved policy, the assignment
1094 * to policy->cpu is like an update of the existing policy, rather than
1095 * the creation of a brand new one. So we need to perform this update
1096 * by invoking update_policy_cpu().
1097 */
1098 if (frozen && cpu != policy->cpu)
1099 update_policy_cpu(policy, cpu);
1100 else
1101 policy->cpu = cpu;
1102
Rusty Russell835481d2009-01-04 05:18:06 -08001103 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 init_completion(&policy->kobj_unregister);
David Howells65f27f32006-11-22 14:55:48 +00001106 INIT_WORK(&policy->update, handle_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 /* call driver. From then on the cpufreq must be able
1109 * to accept all calls to ->verify and ->setpolicy for this CPU
1110 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001111 ret = cpufreq_driver->init(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001113 pr_debug("initialization failed\n");
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301114 goto err_set_policy_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001116
Viresh Kumar5a7e56a2014-03-04 11:44:00 +08001117 /* related cpus should atleast have policy->cpus */
1118 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
1119
1120 /*
1121 * affected cpus must always be the one, which are online. We aren't
1122 * managing offline cpus here.
1123 */
1124 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1125
1126 if (!frozen) {
1127 policy->user_policy.min = policy->min;
1128 policy->user_policy.max = policy->max;
1129 }
1130
Viresh Kumar4e97b632014-03-04 11:44:01 +08001131 down_write(&policy->rwsem);
Viresh Kumar652ed952014-01-09 20:38:43 +05301132 write_lock_irqsave(&cpufreq_driver_lock, flags);
1133 for_each_cpu(j, policy->cpus)
1134 per_cpu(cpufreq_cpu_data, j) = policy;
1135 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1136
Viresh Kumarda60ce92013-10-03 20:28:30 +05301137 if (cpufreq_driver->get) {
1138 policy->cur = cpufreq_driver->get(policy->cpu);
1139 if (!policy->cur) {
1140 pr_err("%s: ->get() failed\n", __func__);
1141 goto err_get_freq;
1142 }
1143 }
1144
Viresh Kumard3916692013-12-03 11:20:46 +05301145 /*
1146 * Sometimes boot loaders set CPU frequency to a value outside of
1147 * frequency table present with cpufreq core. In such cases CPU might be
1148 * unstable if it has to run on that frequency for long duration of time
1149 * and so its better to set it to a frequency which is specified in
1150 * freq-table. This also makes cpufreq stats inconsistent as
1151 * cpufreq-stats would fail to register because current frequency of CPU
1152 * isn't found in freq-table.
1153 *
1154 * Because we don't want this change to effect boot process badly, we go
1155 * for the next freq which is >= policy->cur ('cur' must be set by now,
1156 * otherwise we will end up setting freq to lowest of the table as 'cur'
1157 * is initialized to zero).
1158 *
1159 * We are passing target-freq as "policy->cur - 1" otherwise
1160 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1161 * equal to target-freq.
1162 */
1163 if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1164 && has_target()) {
1165 /* Are we running at unknown frequency ? */
1166 ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1167 if (ret == -EINVAL) {
1168 /* Warn user and fix it */
1169 pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1170 __func__, policy->cpu, policy->cur);
1171 ret = __cpufreq_driver_target(policy, policy->cur - 1,
1172 CPUFREQ_RELATION_L);
1173
1174 /*
1175 * Reaching here after boot in a few seconds may not
1176 * mean that system will remain stable at "unknown"
1177 * frequency for longer duration. Hence, a BUG_ON().
1178 */
1179 BUG_ON(ret);
1180 pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1181 __func__, policy->cpu, policy->cur);
1182 }
1183 }
1184
Thomas Renningera1531ac2008-07-29 22:32:58 -07001185 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1186 CPUFREQ_START, policy);
1187
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301188 if (!frozen) {
Viresh Kumar308b60e2013-07-31 14:35:14 +02001189 ret = cpufreq_add_dev_interface(policy, dev);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301190 if (ret)
1191 goto err_out_unregister;
Viresh Kumarfcd7af92014-01-07 07:10:10 +05301192 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1193 CPUFREQ_CREATE_POLICY, policy);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301194 }
Dave Jones8ff69732006-03-05 03:37:23 -05001195
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301196 write_lock_irqsave(&cpufreq_driver_lock, flags);
1197 list_add(&policy->policy_list, &cpufreq_policy_list);
1198 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1199
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301200 cpufreq_init_policy(policy);
1201
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301202 if (!frozen) {
1203 policy->user_policy.policy = policy->policy;
1204 policy->user_policy.governor = policy->governor;
1205 }
Viresh Kumar4e97b632014-03-04 11:44:01 +08001206 up_write(&policy->rwsem);
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301207
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001208 kobject_uevent(&policy->kobj, KOBJ_ADD);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301209 up_read(&cpufreq_rwsem);
1210
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001211 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 return 0;
1214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215err_out_unregister:
Viresh Kumar652ed952014-01-09 20:38:43 +05301216err_get_freq:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001217 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar474deff2013-08-20 12:08:25 +05301218 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -07001219 per_cpu(cpufreq_cpu_data, j) = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001220 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Viresh Kumarda60ce92013-10-03 20:28:30 +05301222 if (cpufreq_driver->exit)
1223 cpufreq_driver->exit(policy);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301224err_set_policy_cpu:
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001225 if (frozen) {
1226 /* Do not leave stale fallback data behind. */
1227 per_cpu(cpufreq_cpu_data_fallback, cpu) = NULL;
Viresh Kumar42f921a2013-12-20 21:26:02 +05301228 cpufreq_policy_put_kobj(policy);
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001229 }
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301230 cpufreq_policy_free(policy);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232nomem_out:
Viresh Kumar6eed9402013-08-06 22:53:11 +05301233 up_read(&cpufreq_rwsem);
1234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 return ret;
1236}
1237
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301238/**
1239 * cpufreq_add_dev - add a CPU device
1240 *
1241 * Adds the cpufreq interface for a CPU device.
1242 *
1243 * The Oracle says: try running cpufreq registration/unregistration concurrently
1244 * with with cpu hotplugging and all hell will break loose. Tried to clean this
1245 * mess up, but more thorough testing is needed. - Mathieu
1246 */
1247static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1248{
1249 return __cpufreq_add_dev(dev, sif, false);
1250}
1251
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301252static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy *policy,
Viresh Kumar42f921a2013-12-20 21:26:02 +05301253 unsigned int old_cpu)
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301254{
1255 struct device *cpu_dev;
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301256 int ret;
1257
1258 /* first sibling now owns the new sysfs dir */
Viresh Kumar9c8f1ee2013-09-12 17:06:33 +05301259 cpu_dev = get_cpu_device(cpumask_any_but(policy->cpus, old_cpu));
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301260
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301261 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301262 ret = kobject_move(&policy->kobj, &cpu_dev->kobj);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301263 if (ret) {
1264 pr_err("%s: Failed to move kobj: %d", __func__, ret);
1265
viresh kumarad7722d2013-10-18 19:10:15 +05301266 down_write(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301267 cpumask_set_cpu(old_cpu, policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301268 up_write(&policy->rwsem);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301269
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301270 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301271 "cpufreq");
1272
1273 return -EINVAL;
1274 }
1275
1276 return cpu_dev->id;
1277}
1278
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301279static int __cpufreq_remove_dev_prepare(struct device *dev,
1280 struct subsys_interface *sif,
1281 bool frozen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301283 unsigned int cpu = dev->id, cpus;
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301284 int new_cpu, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 unsigned long flags;
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301286 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001288 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001290 write_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301292 policy = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301294 /* Save the policy somewhere when doing a light-weight tear-down */
1295 if (frozen)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301296 per_cpu(cpufreq_cpu_data_fallback, cpu) = policy;
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301297
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001298 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301300 if (!policy) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001301 pr_debug("%s: No cpu_data found\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301305 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301306 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1307 if (ret) {
1308 pr_err("%s: Failed to stop governor\n", __func__);
1309 return ret;
1310 }
1311 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001312
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001313 if (!cpufreq_driver->setpolicy)
Dirk Brandewiefa69e332013-02-06 09:02:11 -08001314 strncpy(per_cpu(cpufreq_cpu_governor, cpu),
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301315 policy->governor->name, CPUFREQ_NAME_LEN);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001316
viresh kumarad7722d2013-10-18 19:10:15 +05301317 down_read(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301318 cpus = cpumask_weight(policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301319 up_read(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Srivatsa S. Bhat61173f22013-09-12 01:43:25 +05301321 if (cpu != policy->cpu) {
viresh kumar6964d912014-02-17 14:52:11 +05301322 sysfs_remove_link(&dev->kobj, "cpufreq");
Viresh Kumar73bf0fc2013-02-05 22:21:14 +01001323 } else if (cpus > 1) {
Viresh Kumar42f921a2013-12-20 21:26:02 +05301324 new_cpu = cpufreq_nominate_new_policy_cpu(policy, cpu);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301325 if (new_cpu >= 0) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301326 update_policy_cpu(policy, new_cpu);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301327
1328 if (!frozen) {
Viresh Kumar75949c92013-10-02 14:13:13 +05301329 pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
1330 __func__, new_cpu, cpu);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301331 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001332 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001333 }
Venki Pallipadiec282972007-03-26 12:03:19 -07001334
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301335 return 0;
1336}
1337
1338static int __cpufreq_remove_dev_finish(struct device *dev,
1339 struct subsys_interface *sif,
1340 bool frozen)
1341{
1342 unsigned int cpu = dev->id, cpus;
1343 int ret;
1344 unsigned long flags;
1345 struct cpufreq_policy *policy;
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301346
1347 read_lock_irqsave(&cpufreq_driver_lock, flags);
1348 policy = per_cpu(cpufreq_cpu_data, cpu);
1349 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1350
1351 if (!policy) {
1352 pr_debug("%s: No cpu_data found\n", __func__);
1353 return -EINVAL;
1354 }
1355
viresh kumarad7722d2013-10-18 19:10:15 +05301356 down_write(&policy->rwsem);
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301357 cpus = cpumask_weight(policy->cpus);
Viresh Kumar9c8f1ee2013-09-12 17:06:33 +05301358
1359 if (cpus > 1)
1360 cpumask_clear_cpu(cpu, policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301361 up_write(&policy->rwsem);
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301362
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001363 /* If cpu is last user of policy, free policy */
1364 if (cpus == 1) {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301365 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301366 ret = __cpufreq_governor(policy,
1367 CPUFREQ_GOV_POLICY_EXIT);
1368 if (ret) {
1369 pr_err("%s: Failed to exit governor\n",
1370 __func__);
1371 return ret;
1372 }
Viresh Kumaredab2fb2013-08-20 12:08:22 +05301373 }
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001374
Viresh Kumar42f921a2013-12-20 21:26:02 +05301375 if (!frozen)
1376 cpufreq_policy_put_kobj(policy);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301377
1378 /*
1379 * Perform the ->exit() even during light-weight tear-down,
1380 * since this is a core component, and is essential for the
1381 * subsequent light-weight ->init() to succeed.
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001382 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001383 if (cpufreq_driver->exit)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301384 cpufreq_driver->exit(policy);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001385
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301386 /* Remove policy from list of active policies */
1387 write_lock_irqsave(&cpufreq_driver_lock, flags);
1388 list_del(&policy->policy_list);
1389 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1390
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301391 if (!frozen)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301392 cpufreq_policy_free(policy);
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001393 } else {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301394 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301395 if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
1396 (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
1397 pr_err("%s: Failed to start governor\n",
1398 __func__);
1399 return ret;
1400 }
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001401 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Viresh Kumar474deff2013-08-20 12:08:25 +05301404 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 return 0;
1406}
1407
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301408/**
Viresh Kumar27a862e2013-10-02 14:13:14 +05301409 * cpufreq_remove_dev - remove a CPU device
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301410 *
1411 * Removes the cpufreq interface for a CPU device.
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301412 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001413static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001414{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001415 unsigned int cpu = dev->id;
Viresh Kumar27a862e2013-10-02 14:13:14 +05301416 int ret;
Venki Pallipadiec282972007-03-26 12:03:19 -07001417
1418 if (cpu_is_offline(cpu))
1419 return 0;
1420
Viresh Kumar27a862e2013-10-02 14:13:14 +05301421 ret = __cpufreq_remove_dev_prepare(dev, sif, false);
1422
1423 if (!ret)
1424 ret = __cpufreq_remove_dev_finish(dev, sif, false);
1425
1426 return ret;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001427}
1428
David Howells65f27f32006-11-22 14:55:48 +00001429static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
David Howells65f27f32006-11-22 14:55:48 +00001431 struct cpufreq_policy *policy =
1432 container_of(work, struct cpufreq_policy, update);
1433 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001434 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 cpufreq_update_policy(cpu);
1436}
1437
1438/**
Viresh Kumarbb176f72013-06-19 14:19:33 +05301439 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1440 * in deep trouble.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 * @cpu: cpu number
1442 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1443 * @new_freq: CPU frequency the CPU actually runs at
1444 *
Dave Jones29464f22009-01-18 01:37:11 -05001445 * We adjust to current frequency first, and need to clean up later.
1446 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301448static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1449 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301451 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301453 unsigned long flags;
1454
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001455 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 freqs.old = old_freq;
1459 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301460
1461 read_lock_irqsave(&cpufreq_driver_lock, flags);
1462 policy = per_cpu(cpufreq_cpu_data, cpu);
1463 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1464
1465 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1466 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467}
1468
Dave Jones32ee8c32006-02-28 00:43:23 -05001469/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301470 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001471 * @cpu: CPU number
1472 *
1473 * This is the last known freq, without actually getting it from the driver.
1474 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1475 */
1476unsigned int cpufreq_quick_get(unsigned int cpu)
1477{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001478 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301479 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001480
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001481 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1482 return cpufreq_driver->get(cpu);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001483
1484 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001485 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301486 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001487 cpufreq_cpu_put(policy);
1488 }
1489
Dave Jones4d34a672008-02-07 16:33:49 -05001490 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001491}
1492EXPORT_SYMBOL(cpufreq_quick_get);
1493
Jesse Barnes3d737102011-06-28 10:59:12 -07001494/**
1495 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1496 * @cpu: CPU number
1497 *
1498 * Just return the max possible frequency for a given CPU.
1499 */
1500unsigned int cpufreq_quick_get_max(unsigned int cpu)
1501{
1502 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1503 unsigned int ret_freq = 0;
1504
1505 if (policy) {
1506 ret_freq = policy->max;
1507 cpufreq_cpu_put(policy);
1508 }
1509
1510 return ret_freq;
1511}
1512EXPORT_SYMBOL(cpufreq_quick_get_max);
1513
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001514static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001516 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301517 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001519 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001520 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001522 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301524 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001525 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301526 /* verify no discrepancy between actual and
1527 saved value exists */
1528 if (unlikely(ret_freq != policy->cur)) {
1529 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 schedule_work(&policy->update);
1531 }
1532 }
1533
Dave Jones4d34a672008-02-07 16:33:49 -05001534 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001535}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001537/**
1538 * cpufreq_get - get the current CPU frequency (in kHz)
1539 * @cpu: CPU number
1540 *
1541 * Get the CPU current (static) CPU frequency
1542 */
1543unsigned int cpufreq_get(unsigned int cpu)
1544{
Aaron Plattner999976e2014-03-04 12:42:15 -08001545 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001546 unsigned int ret_freq = 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001547
Aaron Plattner999976e2014-03-04 12:42:15 -08001548 if (policy) {
1549 down_read(&policy->rwsem);
1550 ret_freq = __cpufreq_get(cpu);
1551 up_read(&policy->rwsem);
Viresh Kumar26ca8692013-09-20 22:37:31 +05301552
Aaron Plattner999976e2014-03-04 12:42:15 -08001553 cpufreq_cpu_put(policy);
1554 }
Viresh Kumar6eed9402013-08-06 22:53:11 +05301555
Dave Jones4d34a672008-02-07 16:33:49 -05001556 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557}
1558EXPORT_SYMBOL(cpufreq_get);
1559
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001560static struct subsys_interface cpufreq_interface = {
1561 .name = "cpufreq",
1562 .subsys = &cpu_subsys,
1563 .add_dev = cpufreq_add_dev,
1564 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001565};
1566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001568 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1569 *
1570 * This function is only executed for the boot processor. The other CPUs
1571 * have been put offline by means of CPU hotplug.
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001572 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001573static int cpufreq_bp_suspend(void)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001574{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301575 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001576
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001577 int cpu = smp_processor_id();
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301578 struct cpufreq_policy *policy;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001579
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001580 pr_debug("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001581
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001582 /* If there's no policy for the boot CPU, we have nothing to do. */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301583 policy = cpufreq_cpu_get(cpu);
1584 if (!policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001585 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001586
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001587 if (cpufreq_driver->suspend) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301588 ret = cpufreq_driver->suspend(policy);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001589 if (ret)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001590 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301591 "step on CPU %u\n", policy->cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001592 }
1593
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301594 cpufreq_cpu_put(policy);
Dave Jonesc9060492008-02-07 16:32:18 -05001595 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001596}
1597
1598/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001599 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 *
1601 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001602 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1603 * restored. It will verify that the current freq is in sync with
1604 * what we believe it to be. This is a bit later than when it
1605 * should be, but nonethteless it's better than calling
1606 * cpufreq_driver->get() here which might re-enable interrupts...
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001607 *
1608 * This function is only executed for the boot CPU. The other CPUs have not
1609 * been turned on yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001611static void cpufreq_bp_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301613 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001614
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001615 int cpu = smp_processor_id();
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301616 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001618 pr_debug("resuming cpu %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001620 /* If there's no policy for the boot CPU, we have nothing to do. */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301621 policy = cpufreq_cpu_get(cpu);
1622 if (!policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001623 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001625 if (cpufreq_driver->resume) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301626 ret = cpufreq_driver->resume(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 if (ret) {
1628 printk(KERN_ERR "cpufreq: resume failed in ->resume "
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301629 "step on CPU %u\n", policy->cpu);
Dave Jonesc9060492008-02-07 16:32:18 -05001630 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 }
1632 }
1633
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301634 schedule_work(&policy->update);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001635
Dave Jonesc9060492008-02-07 16:32:18 -05001636fail:
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301637 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638}
1639
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001640static struct syscore_ops cpufreq_syscore_ops = {
1641 .suspend = cpufreq_bp_suspend,
1642 .resume = cpufreq_bp_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643};
1644
Borislav Petkov9d950462013-01-20 10:24:28 +00001645/**
1646 * cpufreq_get_current_driver - return current driver's name
1647 *
1648 * Return the name string of the currently loaded cpufreq driver
1649 * or NULL, if none.
1650 */
1651const char *cpufreq_get_current_driver(void)
1652{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001653 if (cpufreq_driver)
1654 return cpufreq_driver->name;
1655
1656 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001657}
1658EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660/*********************************************************************
1661 * NOTIFIER LISTS INTERFACE *
1662 *********************************************************************/
1663
1664/**
1665 * cpufreq_register_notifier - register a driver with cpufreq
1666 * @nb: notifier function to register
1667 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1668 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001669 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 * are notified about clock rate changes (once before and once after
1671 * the transition), or a list of drivers that are notified about
1672 * changes in cpufreq policy.
1673 *
1674 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001675 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 */
1677int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1678{
1679 int ret;
1680
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001681 if (cpufreq_disabled())
1682 return -EINVAL;
1683
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001684 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 switch (list) {
1687 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001688 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001689 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 break;
1691 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001692 ret = blocking_notifier_chain_register(
1693 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 break;
1695 default:
1696 ret = -EINVAL;
1697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
1699 return ret;
1700}
1701EXPORT_SYMBOL(cpufreq_register_notifier);
1702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703/**
1704 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1705 * @nb: notifier block to be unregistered
Viresh Kumarbb176f72013-06-19 14:19:33 +05301706 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 *
1708 * Remove a driver from the CPU frequency notifier list.
1709 *
1710 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001711 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 */
1713int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1714{
1715 int ret;
1716
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001717 if (cpufreq_disabled())
1718 return -EINVAL;
1719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 switch (list) {
1721 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001722 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001723 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 break;
1725 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001726 ret = blocking_notifier_chain_unregister(
1727 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 break;
1729 default:
1730 ret = -EINVAL;
1731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 return ret;
1734}
1735EXPORT_SYMBOL(cpufreq_unregister_notifier);
1736
1737
1738/*********************************************************************
1739 * GOVERNORS *
1740 *********************************************************************/
1741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742int __cpufreq_driver_target(struct cpufreq_policy *policy,
1743 unsigned int target_freq,
1744 unsigned int relation)
1745{
1746 int retval = -EINVAL;
Viresh Kumar72499242012-10-31 01:28:21 +01001747 unsigned int old_target_freq = target_freq;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001748
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001749 if (cpufreq_disabled())
1750 return -ENODEV;
1751
Viresh Kumar72499242012-10-31 01:28:21 +01001752 /* Make sure that target_freq is within supported range */
1753 if (target_freq > policy->max)
1754 target_freq = policy->max;
1755 if (target_freq < policy->min)
1756 target_freq = policy->min;
1757
1758 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1759 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001760
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301761 /*
1762 * This might look like a redundant call as we are checking it again
1763 * after finding index. But it is left intentionally for cases where
1764 * exactly same freq is called again and so we can save on few function
1765 * calls.
1766 */
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001767 if (target_freq == policy->cur)
1768 return 0;
1769
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001770 if (cpufreq_driver->target)
1771 retval = cpufreq_driver->target(policy, target_freq, relation);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301772 else if (cpufreq_driver->target_index) {
1773 struct cpufreq_frequency_table *freq_table;
Viresh Kumard4019f02013-08-14 19:38:24 +05301774 struct cpufreq_freqs freqs;
1775 bool notify;
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301776 int index;
Ashok Raj90d45d12005-11-08 21:34:24 -08001777
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301778 freq_table = cpufreq_frequency_get_table(policy->cpu);
1779 if (unlikely(!freq_table)) {
1780 pr_err("%s: Unable to find freq_table\n", __func__);
1781 goto out;
1782 }
1783
1784 retval = cpufreq_frequency_table_target(policy, freq_table,
1785 target_freq, relation, &index);
1786 if (unlikely(retval)) {
1787 pr_err("%s: Unable to find matching freq\n", __func__);
1788 goto out;
1789 }
1790
Viresh Kumard4019f02013-08-14 19:38:24 +05301791 if (freq_table[index].frequency == policy->cur) {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301792 retval = 0;
Viresh Kumard4019f02013-08-14 19:38:24 +05301793 goto out;
1794 }
1795
1796 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
1797
1798 if (notify) {
1799 freqs.old = policy->cur;
1800 freqs.new = freq_table[index].frequency;
1801 freqs.flags = 0;
1802
1803 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1804 __func__, policy->cpu, freqs.old,
1805 freqs.new);
1806
1807 cpufreq_notify_transition(policy, &freqs,
1808 CPUFREQ_PRECHANGE);
1809 }
1810
1811 retval = cpufreq_driver->target_index(policy, index);
1812 if (retval)
1813 pr_err("%s: Failed to change cpu frequency: %d\n",
1814 __func__, retval);
1815
Viresh Kumarab1b1c42013-12-02 11:04:13 +05301816 if (notify)
1817 cpufreq_notify_post_transition(policy, &freqs, retval);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301818 }
1819
1820out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 return retval;
1822}
1823EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825int cpufreq_driver_target(struct cpufreq_policy *policy,
1826 unsigned int target_freq,
1827 unsigned int relation)
1828{
Julia Lawallf1829e42008-07-25 22:44:53 +02001829 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
viresh kumarad7722d2013-10-18 19:10:15 +05301831 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
1833 ret = __cpufreq_driver_target(policy, target_freq, relation);
1834
viresh kumarad7722d2013-10-18 19:10:15 +05301835 up_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 return ret;
1838}
1839EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1840
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001841/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001842 * when "event" is CPUFREQ_GOV_LIMITS
1843 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301845static int __cpufreq_governor(struct cpufreq_policy *policy,
1846 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847{
Dave Jonescc993ca2005-07-28 09:43:56 -07001848 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001849
1850 /* Only must be defined when default governor is known to have latency
1851 restrictions, like e.g. conservative or ondemand.
1852 That this is the case is already ensured in Kconfig
1853 */
1854#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1855 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1856#else
1857 struct cpufreq_governor *gov = NULL;
1858#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001859
1860 if (policy->governor->max_transition_latency &&
1861 policy->cpuinfo.transition_latency >
1862 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07001863 if (!gov)
1864 return -EINVAL;
1865 else {
1866 printk(KERN_WARNING "%s governor failed, too long"
1867 " transition latency of HW, fallback"
1868 " to %s governor\n",
1869 policy->governor->name,
1870 gov->name);
1871 policy->governor = gov;
1872 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Viresh Kumarfe492f32013-08-06 22:53:10 +05301875 if (event == CPUFREQ_GOV_POLICY_INIT)
1876 if (!try_module_get(policy->governor->owner))
1877 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001879 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301880 policy->cpu, event);
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001881
1882 mutex_lock(&cpufreq_governor_lock);
Srivatsa S. Bhat56d07db2013-09-07 01:23:55 +05301883 if ((policy->governor_enabled && event == CPUFREQ_GOV_START)
Viresh Kumarf73d3932013-08-31 17:53:40 +05301884 || (!policy->governor_enabled
1885 && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) {
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001886 mutex_unlock(&cpufreq_governor_lock);
1887 return -EBUSY;
1888 }
1889
1890 if (event == CPUFREQ_GOV_STOP)
1891 policy->governor_enabled = false;
1892 else if (event == CPUFREQ_GOV_START)
1893 policy->governor_enabled = true;
1894
1895 mutex_unlock(&cpufreq_governor_lock);
1896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 ret = policy->governor->governor(policy, event);
1898
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001899 if (!ret) {
1900 if (event == CPUFREQ_GOV_POLICY_INIT)
1901 policy->governor->initialized++;
1902 else if (event == CPUFREQ_GOV_POLICY_EXIT)
1903 policy->governor->initialized--;
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001904 } else {
1905 /* Restore original values */
1906 mutex_lock(&cpufreq_governor_lock);
1907 if (event == CPUFREQ_GOV_STOP)
1908 policy->governor_enabled = true;
1909 else if (event == CPUFREQ_GOV_START)
1910 policy->governor_enabled = false;
1911 mutex_unlock(&cpufreq_governor_lock);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001912 }
Viresh Kumarb3940582013-02-01 05:42:58 +00001913
Viresh Kumarfe492f32013-08-06 22:53:10 +05301914 if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
1915 ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 module_put(policy->governor->owner);
1917
1918 return ret;
1919}
1920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921int cpufreq_register_governor(struct cpufreq_governor *governor)
1922{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001923 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
1925 if (!governor)
1926 return -EINVAL;
1927
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001928 if (cpufreq_disabled())
1929 return -ENODEV;
1930
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001931 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001932
Viresh Kumarb3940582013-02-01 05:42:58 +00001933 governor->initialized = 0;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001934 err = -EBUSY;
1935 if (__find_governor(governor->name) == NULL) {
1936 err = 0;
1937 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Dave Jones32ee8c32006-02-28 00:43:23 -05001940 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001941 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942}
1943EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1946{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001947 int cpu;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001948
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 if (!governor)
1950 return;
1951
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001952 if (cpufreq_disabled())
1953 return;
1954
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001955 for_each_present_cpu(cpu) {
1956 if (cpu_online(cpu))
1957 continue;
1958 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1959 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1960 }
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001961
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001962 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001964 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 return;
1966}
1967EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1968
1969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970/*********************************************************************
1971 * POLICY INTERFACE *
1972 *********************************************************************/
1973
1974/**
1975 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05001976 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1977 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 *
1979 * Reads the current cpufreq policy.
1980 */
1981int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1982{
1983 struct cpufreq_policy *cpu_policy;
1984 if (!policy)
1985 return -EINVAL;
1986
1987 cpu_policy = cpufreq_cpu_get(cpu);
1988 if (!cpu_policy)
1989 return -EINVAL;
1990
Viresh Kumard5b73cd2013-08-06 22:53:06 +05301991 memcpy(policy, cpu_policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
1993 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 return 0;
1995}
1996EXPORT_SYMBOL(cpufreq_get_policy);
1997
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001998/*
Viresh Kumar037ce832013-10-02 14:13:16 +05301999 * policy : current policy.
2000 * new_policy: policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002001 */
Viresh Kumar037ce832013-10-02 14:13:16 +05302002static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302003 struct cpufreq_policy *new_policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004{
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002005 struct cpufreq_governor *old_gov;
2006 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302008 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", new_policy->cpu,
2009 new_policy->min, new_policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302011 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002013 if (new_policy->min > policy->max || new_policy->max < policy->min)
2014 return -EINVAL;
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02002015
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 /* verify the cpu speed can be set within this limit */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302017 ret = cpufreq_driver->verify(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002019 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08002022 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302023 CPUFREQ_ADJUST, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
2025 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08002026 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302027 CPUFREQ_INCOMPATIBLE, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Viresh Kumarbb176f72013-06-19 14:19:33 +05302029 /*
2030 * verify the cpu speed can be set within this limit, which might be
2031 * different to the first one
2032 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302033 ret = cpufreq_driver->verify(new_policy);
Alan Sterne041c682006-03-27 01:16:30 -08002034 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002035 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
2037 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08002038 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302039 CPUFREQ_NOTIFY, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302041 policy->min = new_policy->min;
2042 policy->max = new_policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002044 pr_debug("new min and max freqs are %u - %u kHz\n",
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302045 policy->min, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002047 if (cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302048 policy->policy = new_policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002049 pr_debug("setting range\n");
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002050 return cpufreq_driver->setpolicy(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 }
2052
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002053 if (new_policy->governor == policy->governor)
2054 goto out;
2055
2056 pr_debug("governor switch\n");
2057
2058 /* save old, working values */
2059 old_gov = policy->governor;
2060 /* end old governor */
2061 if (old_gov) {
2062 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
2063 up_write(&policy->rwsem);
2064 __cpufreq_governor(policy,CPUFREQ_GOV_POLICY_EXIT);
2065 down_write(&policy->rwsem);
2066 }
2067
2068 /* start new governor */
2069 policy->governor = new_policy->governor;
2070 if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
2071 if (!__cpufreq_governor(policy, CPUFREQ_GOV_START))
2072 goto out;
2073
2074 up_write(&policy->rwsem);
2075 __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
2076 down_write(&policy->rwsem);
2077 }
2078
2079 /* new governor failed, so re-start old one */
2080 pr_debug("starting governor %s failed\n", policy->governor->name);
2081 if (old_gov) {
2082 policy->governor = old_gov;
2083 __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT);
2084 __cpufreq_governor(policy, CPUFREQ_GOV_START);
2085 }
2086
2087 return -EINVAL;
2088
2089 out:
2090 pr_debug("governor: change or update limits\n");
2091 return __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092}
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);
Viresh Kumarbd0fa9b2014-02-25 14:29:44 +05302127 if (WARN_ON(!new_policy.cur)) {
2128 ret = -EIO;
2129 goto no_policy;
2130 }
2131
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302132 if (!policy->cur) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002133 pr_debug("Driver did not initialize current freq");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302134 policy->cur = new_policy.cur;
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002135 } else {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302136 if (policy->cur != new_policy.cur && has_target())
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302137 cpufreq_out_of_sync(cpu, policy->cur,
2138 new_policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002139 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01002140 }
2141
Viresh Kumar037ce832013-10-02 14:13:16 +05302142 ret = cpufreq_set_policy(policy, &new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
viresh kumarad7722d2013-10-18 19:10:15 +05302144 up_write(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002145
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302146 cpufreq_cpu_put(policy);
Julia Lawallf1829e42008-07-25 22:44:53 +02002147no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 return ret;
2149}
2150EXPORT_SYMBOL(cpufreq_update_policy);
2151
Paul Gortmaker27609842013-06-19 13:54:04 -04002152static int cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002153 unsigned long action, void *hcpu)
2154{
2155 unsigned int cpu = (unsigned long)hcpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002156 struct device *dev;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302157 bool frozen = false;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002158
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002159 dev = get_cpu_device(cpu);
2160 if (dev) {
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302161
Rafael J. Wysockid4faadd2013-12-08 01:23:58 +01002162 if (action & CPU_TASKS_FROZEN)
2163 frozen = true;
2164
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302165 switch (action & ~CPU_TASKS_FROZEN) {
Ashok Rajc32b6b82005-10-30 14:59:54 -08002166 case CPU_ONLINE:
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302167 __cpufreq_add_dev(dev, NULL, frozen);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002168 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302169
Ashok Rajc32b6b82005-10-30 14:59:54 -08002170 case CPU_DOWN_PREPARE:
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05302171 __cpufreq_remove_dev_prepare(dev, NULL, frozen);
Srivatsa S. Bhat1aee40a2013-09-07 01:23:27 +05302172 break;
2173
2174 case CPU_POST_DEAD:
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05302175 __cpufreq_remove_dev_finish(dev, NULL, frozen);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002176 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302177
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002178 case CPU_DOWN_FAILED:
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302179 __cpufreq_add_dev(dev, NULL, frozen);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002180 break;
2181 }
2182 }
2183 return NOTIFY_OK;
2184}
2185
Neal Buckendahl9c36f742010-06-22 22:02:44 -05002186static struct notifier_block __refdata cpufreq_cpu_notifier = {
Viresh Kumarbb176f72013-06-19 14:19:33 +05302187 .notifier_call = cpufreq_cpu_callback,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002188};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
2190/*********************************************************************
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002191 * BOOST *
2192 *********************************************************************/
2193static int cpufreq_boost_set_sw(int state)
2194{
2195 struct cpufreq_frequency_table *freq_table;
2196 struct cpufreq_policy *policy;
2197 int ret = -EINVAL;
2198
2199 list_for_each_entry(policy, &cpufreq_policy_list, policy_list) {
2200 freq_table = cpufreq_frequency_get_table(policy->cpu);
2201 if (freq_table) {
2202 ret = cpufreq_frequency_table_cpuinfo(policy,
2203 freq_table);
2204 if (ret) {
2205 pr_err("%s: Policy frequency update failed\n",
2206 __func__);
2207 break;
2208 }
2209 policy->user_policy.max = policy->max;
2210 __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
2211 }
2212 }
2213
2214 return ret;
2215}
2216
2217int cpufreq_boost_trigger_state(int state)
2218{
2219 unsigned long flags;
2220 int ret = 0;
2221
2222 if (cpufreq_driver->boost_enabled == state)
2223 return 0;
2224
2225 write_lock_irqsave(&cpufreq_driver_lock, flags);
2226 cpufreq_driver->boost_enabled = state;
2227 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2228
2229 ret = cpufreq_driver->set_boost(state);
2230 if (ret) {
2231 write_lock_irqsave(&cpufreq_driver_lock, flags);
2232 cpufreq_driver->boost_enabled = !state;
2233 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2234
2235 pr_err("%s: Cannot %s BOOST\n", __func__,
2236 state ? "enable" : "disable");
2237 }
2238
2239 return ret;
2240}
2241
2242int cpufreq_boost_supported(void)
2243{
2244 if (likely(cpufreq_driver))
2245 return cpufreq_driver->boost_supported;
2246
2247 return 0;
2248}
2249EXPORT_SYMBOL_GPL(cpufreq_boost_supported);
2250
2251int cpufreq_boost_enabled(void)
2252{
2253 return cpufreq_driver->boost_enabled;
2254}
2255EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2256
2257/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2259 *********************************************************************/
2260
2261/**
2262 * cpufreq_register_driver - register a CPU Frequency driver
2263 * @driver_data: A struct cpufreq_driver containing the values#
2264 * submitted by the CPU Frequency driver.
2265 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302266 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05002268 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 *
2270 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002271int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272{
2273 unsigned long flags;
2274 int ret;
2275
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002276 if (cpufreq_disabled())
2277 return -ENODEV;
2278
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 if (!driver_data || !driver_data->verify || !driver_data->init ||
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302280 !(driver_data->setpolicy || driver_data->target_index ||
2281 driver_data->target))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 return -EINVAL;
2283
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002284 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
2286 if (driver_data->setpolicy)
2287 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2288
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002289 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002290 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002291 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Yinghai Lu4dea58062013-09-18 21:05:20 -07002292 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002294 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002295 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002297 if (cpufreq_boost_supported()) {
2298 /*
2299 * Check if driver provides function to enable boost -
2300 * if not, use cpufreq_boost_set_sw as default
2301 */
2302 if (!cpufreq_driver->set_boost)
2303 cpufreq_driver->set_boost = cpufreq_boost_set_sw;
2304
2305 ret = cpufreq_sysfs_create_file(&boost.attr);
2306 if (ret) {
2307 pr_err("%s: cannot register global BOOST sysfs file\n",
2308 __func__);
2309 goto err_null_driver;
2310 }
2311 }
2312
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002313 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002314 if (ret)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002315 goto err_boost_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002317 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 int i;
2319 ret = -ENODEV;
2320
2321 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07002322 for (i = 0; i < nr_cpu_ids; i++)
2323 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07002325 break;
2326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
2328 /* if all ->init() calls failed, unregister */
2329 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002330 pr_debug("no CPU initialized for driver %s\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05302331 driver_data->name);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002332 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 }
2334 }
2335
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002336 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002337 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002339 return 0;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002340err_if_unreg:
2341 subsys_interface_unregister(&cpufreq_interface);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002342err_boost_unreg:
2343 if (cpufreq_boost_supported())
2344 cpufreq_sysfs_remove_file(&boost.attr);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002345err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002346 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002347 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002348 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05002349 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350}
2351EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2352
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353/**
2354 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2355 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302356 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 * the right to do so, i.e. if you have succeeded in initialising before!
2358 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2359 * currently not initialised.
2360 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002361int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362{
2363 unsigned long flags;
2364
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002365 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002368 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002370 subsys_interface_unregister(&cpufreq_interface);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002371 if (cpufreq_boost_supported())
2372 cpufreq_sysfs_remove_file(&boost.attr);
2373
Chandra Seetharaman65edc682006-06-27 02:54:08 -07002374 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Viresh Kumar6eed9402013-08-06 22:53:11 +05302376 down_write(&cpufreq_rwsem);
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002377 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302378
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002379 cpufreq_driver = NULL;
Viresh Kumar6eed9402013-08-06 22:53:11 +05302380
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002381 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302382 up_write(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
2384 return 0;
2385}
2386EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002387
2388static int __init cpufreq_core_init(void)
2389{
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002390 if (cpufreq_disabled())
2391 return -ENODEV;
2392
Viresh Kumar2361be22013-05-17 16:09:09 +05302393 cpufreq_global_kobject = kobject_create();
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002394 BUG_ON(!cpufreq_global_kobject);
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01002395 register_syscore_ops(&cpufreq_syscore_ops);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002396
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002397 return 0;
2398}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002399core_initcall(cpufreq_core_init);