cpufreq: Don't use smp_processor_id() in preemptible context
[deliverable/linux.git] / drivers / cpufreq / cpufreq.c
CommitLineData
1da177e4
LT
1/*
2 * linux/drivers/cpufreq/cpufreq.c
3 *
4 * Copyright (C) 2001 Russell King
5 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
bb176f7d 6 * (C) 2013 Viresh Kumar <viresh.kumar@linaro.org>
1da177e4 7 *
c32b6b8e 8 * Oct 2005 - Ashok Raj <ashok.raj@intel.com>
32ee8c3e 9 * Added handling for CPU hotplug
8ff69732
DJ
10 * Feb 2006 - Jacob Shin <jacob.shin@amd.com>
11 * Fix handling for CPU hotplug -- affected CPUs
c32b6b8e 12 *
1da177e4
LT
13 * 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.
1da177e4
LT
16 */
17
db701151
VK
18#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
5ff0a268 20#include <linux/cpu.h>
1da177e4
LT
21#include <linux/cpufreq.h>
22#include <linux/delay.h>
1da177e4 23#include <linux/device.h>
5ff0a268
VK
24#include <linux/init.h>
25#include <linux/kernel_stat.h>
26#include <linux/module.h>
3fc54d37 27#include <linux/mutex.h>
5ff0a268 28#include <linux/slab.h>
e00e56df 29#include <linux/syscore_ops.h>
5ff0a268 30#include <linux/tick.h>
6f4f2723
TR
31#include <trace/events/power.h>
32
1da177e4 33/**
cd878479 34 * The "cpufreq driver" - the arch- or hardware-dependent low
1da177e4
LT
35 * level driver of CPUFreq support, and its spinlock. This lock
36 * also protects the cpufreq_cpu_data array.
37 */
1c3d85dd 38static struct cpufreq_driver *cpufreq_driver;
7a6aedfa 39static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
8414809c 40static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data_fallback);
bb176f7d
VK
41static DEFINE_RWLOCK(cpufreq_driver_lock);
42static DEFINE_MUTEX(cpufreq_governor_lock);
c88a1f8b 43static LIST_HEAD(cpufreq_policy_list);
bb176f7d 44
084f3493
TR
45#ifdef CONFIG_HOTPLUG_CPU
46/* This one keeps track of the previously set governor of a removed CPU */
e77b89f1 47static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
084f3493 48#endif
1da177e4 49
5a01f2e8
VP
50/*
51 * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
52 * all cpufreq/hotplug/workqueue/etc related lock issues.
53 *
54 * The rules for this semaphore:
55 * - Any routine that wants to read from the policy structure will
56 * do a down_read on this semaphore.
57 * - Any routine that will write to the policy structure and/or may take away
58 * the policy altogether (eg. CPU hotplug), will hold this lock in write
59 * mode before doing so.
60 *
61 * Additional rules:
5a01f2e8
VP
62 * - Governor routines that can be called in cpufreq hotplug path should not
63 * take this sem as top level hotplug notifier handler takes this.
395913d0
MD
64 * - Lock should not be held across
65 * __cpufreq_governor(data, CPUFREQ_GOV_STOP);
5a01f2e8 66 */
5a01f2e8
VP
67static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
68
69#define lock_policy_rwsem(mode, cpu) \
fa1d8af4 70static int lock_policy_rwsem_##mode(int cpu) \
5a01f2e8 71{ \
474deff7
VK
72 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); \
73 BUG_ON(!policy); \
74 down_##mode(&per_cpu(cpu_policy_rwsem, policy->cpu)); \
5a01f2e8
VP
75 \
76 return 0; \
77}
78
79lock_policy_rwsem(read, cpu);
5a01f2e8 80lock_policy_rwsem(write, cpu);
5a01f2e8 81
fa1d8af4
VK
82#define unlock_policy_rwsem(mode, cpu) \
83static void unlock_policy_rwsem_##mode(int cpu) \
84{ \
474deff7
VK
85 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); \
86 BUG_ON(!policy); \
87 up_##mode(&per_cpu(cpu_policy_rwsem, policy->cpu)); \
5a01f2e8 88}
5a01f2e8 89
fa1d8af4
VK
90unlock_policy_rwsem(read, cpu);
91unlock_policy_rwsem(write, cpu);
5a01f2e8 92
6eed9404
VK
93/*
94 * rwsem to guarantee that cpufreq driver module doesn't unload during critical
95 * sections
96 */
97static DECLARE_RWSEM(cpufreq_rwsem);
98
1da177e4 99/* internal prototypes */
29464f28
DJ
100static int __cpufreq_governor(struct cpufreq_policy *policy,
101 unsigned int event);
5a01f2e8 102static unsigned int __cpufreq_get(unsigned int cpu);
65f27f38 103static void handle_update(struct work_struct *work);
1da177e4
LT
104
105/**
32ee8c3e
DJ
106 * Two notifier lists: the "policy" list is involved in the
107 * validation process for a new CPU frequency policy; the
1da177e4
LT
108 * "transition" list for kernel code that needs to handle
109 * changes to devices when the CPU clock speed changes.
110 * The mutex locks both lists.
111 */
e041c683 112static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
b4dfdbb3 113static struct srcu_notifier_head cpufreq_transition_notifier_list;
1da177e4 114
74212ca4 115static bool init_cpufreq_transition_notifier_list_called;
b4dfdbb3
AS
116static int __init init_cpufreq_transition_notifier_list(void)
117{
118 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
74212ca4 119 init_cpufreq_transition_notifier_list_called = true;
b4dfdbb3
AS
120 return 0;
121}
b3438f82 122pure_initcall(init_cpufreq_transition_notifier_list);
1da177e4 123
a7b422cd 124static int off __read_mostly;
da584455 125static int cpufreq_disabled(void)
a7b422cd
KRW
126{
127 return off;
128}
129void disable_cpufreq(void)
130{
131 off = 1;
132}
1da177e4 133static LIST_HEAD(cpufreq_governor_list);
29464f28 134static DEFINE_MUTEX(cpufreq_governor_mutex);
1da177e4 135
4d5dcc42
VK
136bool have_governor_per_policy(void)
137{
1c3d85dd 138 return cpufreq_driver->have_governor_per_policy;
4d5dcc42 139}
3f869d6d 140EXPORT_SYMBOL_GPL(have_governor_per_policy);
4d5dcc42 141
944e9a03
VK
142struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
143{
144 if (have_governor_per_policy())
145 return &policy->kobj;
146 else
147 return cpufreq_global_kobject;
148}
149EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
150
72a4ce34
VK
151static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
152{
153 u64 idle_time;
154 u64 cur_wall_time;
155 u64 busy_time;
156
157 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
158
159 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
160 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
161 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
162 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
163 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
164 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
165
166 idle_time = cur_wall_time - busy_time;
167 if (wall)
168 *wall = cputime_to_usecs(cur_wall_time);
169
170 return cputime_to_usecs(idle_time);
171}
172
173u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
174{
175 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
176
177 if (idle_time == -1ULL)
178 return get_cpu_idle_time_jiffy(cpu, wall);
179 else if (!io_busy)
180 idle_time += get_cpu_iowait_time_us(cpu, wall);
181
182 return idle_time;
183}
184EXPORT_SYMBOL_GPL(get_cpu_idle_time);
185
6eed9404 186struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
1da177e4 187{
6eed9404 188 struct cpufreq_policy *policy = NULL;
1da177e4
LT
189 unsigned long flags;
190
6eed9404
VK
191 if (cpufreq_disabled() || (cpu >= nr_cpu_ids))
192 return NULL;
193
194 if (!down_read_trylock(&cpufreq_rwsem))
195 return NULL;
1da177e4
LT
196
197 /* get the cpufreq driver */
1c3d85dd 198 read_lock_irqsave(&cpufreq_driver_lock, flags);
1da177e4 199
6eed9404
VK
200 if (cpufreq_driver) {
201 /* get the CPU */
202 policy = per_cpu(cpufreq_cpu_data, cpu);
203 if (policy)
204 kobject_get(&policy->kobj);
205 }
1da177e4 206
6eed9404 207 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1da177e4 208
3a3e9e06 209 if (!policy)
6eed9404 210 up_read(&cpufreq_rwsem);
1da177e4 211
3a3e9e06 212 return policy;
a9144436 213}
1da177e4
LT
214EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
215
3a3e9e06 216void cpufreq_cpu_put(struct cpufreq_policy *policy)
1da177e4 217{
d5aaffa9
DB
218 if (cpufreq_disabled())
219 return;
220
6eed9404
VK
221 kobject_put(&policy->kobj);
222 up_read(&cpufreq_rwsem);
1da177e4
LT
223}
224EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
225
1da177e4
LT
226/*********************************************************************
227 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
228 *********************************************************************/
229
230/**
231 * adjust_jiffies - adjust the system "loops_per_jiffy"
232 *
233 * This function alters the system "loops_per_jiffy" for the clock
234 * speed change. Note that loops_per_jiffy cannot be updated on SMP
32ee8c3e 235 * systems as each CPU might be scaled differently. So, use the arch
1da177e4
LT
236 * per-CPU loops_per_jiffy value wherever possible.
237 */
238#ifndef CONFIG_SMP
239static unsigned long l_p_j_ref;
bb176f7d 240static unsigned int l_p_j_ref_freq;
1da177e4 241
858119e1 242static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
1da177e4
LT
243{
244 if (ci->flags & CPUFREQ_CONST_LOOPS)
245 return;
246
247 if (!l_p_j_ref_freq) {
248 l_p_j_ref = loops_per_jiffy;
249 l_p_j_ref_freq = ci->old;
2d06d8c4 250 pr_debug("saving %lu as reference value for loops_per_jiffy; "
e08f5f5b 251 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
1da177e4 252 }
bb176f7d 253 if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
42d4dc3f 254 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
e08f5f5b
GS
255 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
256 ci->new);
2d06d8c4 257 pr_debug("scaling loops_per_jiffy to %lu "
e08f5f5b 258 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
1da177e4
LT
259 }
260}
261#else
e08f5f5b
GS
262static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
263{
264 return;
265}
1da177e4
LT
266#endif
267
0956df9c 268static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
b43a7ffb 269 struct cpufreq_freqs *freqs, unsigned int state)
1da177e4
LT
270{
271 BUG_ON(irqs_disabled());
272
d5aaffa9
DB
273 if (cpufreq_disabled())
274 return;
275
1c3d85dd 276 freqs->flags = cpufreq_driver->flags;
2d06d8c4 277 pr_debug("notification %u of frequency transition to %u kHz\n",
e4472cb3 278 state, freqs->new);
1da177e4 279
1da177e4 280 switch (state) {
e4472cb3 281
1da177e4 282 case CPUFREQ_PRECHANGE:
266c13d7
VK
283 if (WARN(policy->transition_ongoing ==
284 cpumask_weight(policy->cpus),
7c30ed53
VK
285 "In middle of another frequency transition\n"))
286 return;
287
266c13d7 288 policy->transition_ongoing++;
7c30ed53 289
32ee8c3e 290 /* detect if the driver reported a value as "old frequency"
e4472cb3
DJ
291 * which is not equal to what the cpufreq core thinks is
292 * "old frequency".
1da177e4 293 */
1c3d85dd 294 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
e4472cb3
DJ
295 if ((policy) && (policy->cpu == freqs->cpu) &&
296 (policy->cur) && (policy->cur != freqs->old)) {
2d06d8c4 297 pr_debug("Warning: CPU frequency is"
e4472cb3
DJ
298 " %u, cpufreq assumed %u kHz.\n",
299 freqs->old, policy->cur);
300 freqs->old = policy->cur;
1da177e4
LT
301 }
302 }
b4dfdbb3 303 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
e041c683 304 CPUFREQ_PRECHANGE, freqs);
1da177e4
LT
305 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
306 break;
e4472cb3 307
1da177e4 308 case CPUFREQ_POSTCHANGE:
7c30ed53
VK
309 if (WARN(!policy->transition_ongoing,
310 "No frequency transition in progress\n"))
311 return;
312
266c13d7 313 policy->transition_ongoing--;
7c30ed53 314
1da177e4 315 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
2d06d8c4 316 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
6f4f2723 317 (unsigned long)freqs->cpu);
25e41933 318 trace_cpu_frequency(freqs->new, freqs->cpu);
b4dfdbb3 319 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
e041c683 320 CPUFREQ_POSTCHANGE, freqs);
e4472cb3
DJ
321 if (likely(policy) && likely(policy->cpu == freqs->cpu))
322 policy->cur = freqs->new;
1da177e4
LT
323 break;
324 }
1da177e4 325}
bb176f7d 326
b43a7ffb
VK
327/**
328 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
329 * on frequency transition.
330 *
331 * This function calls the transition notifiers and the "adjust_jiffies"
332 * function. It is called twice on all CPU frequency changes that have
333 * external effects.
334 */
335void cpufreq_notify_transition(struct cpufreq_policy *policy,
336 struct cpufreq_freqs *freqs, unsigned int state)
337{
338 for_each_cpu(freqs->cpu, policy->cpus)
339 __cpufreq_notify_transition(policy, freqs, state);
340}
1da177e4
LT
341EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
342
343
1da177e4
LT
344/*********************************************************************
345 * SYSFS INTERFACE *
346 *********************************************************************/
347
3bcb09a3
JF
348static struct cpufreq_governor *__find_governor(const char *str_governor)
349{
350 struct cpufreq_governor *t;
351
352 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
29464f28 353 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
3bcb09a3
JF
354 return t;
355
356 return NULL;
357}
358
1da177e4
LT
359/**
360 * cpufreq_parse_governor - parse a governor string
361 */
905d77cd 362static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
1da177e4
LT
363 struct cpufreq_governor **governor)
364{
3bcb09a3 365 int err = -EINVAL;
1c3d85dd
RW
366
367 if (!cpufreq_driver)
3bcb09a3
JF
368 goto out;
369
1c3d85dd 370 if (cpufreq_driver->setpolicy) {
1da177e4
LT
371 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
372 *policy = CPUFREQ_POLICY_PERFORMANCE;
3bcb09a3 373 err = 0;
e08f5f5b
GS
374 } else if (!strnicmp(str_governor, "powersave",
375 CPUFREQ_NAME_LEN)) {
1da177e4 376 *policy = CPUFREQ_POLICY_POWERSAVE;
3bcb09a3 377 err = 0;
1da177e4 378 }
1c3d85dd 379 } else if (cpufreq_driver->target) {
1da177e4 380 struct cpufreq_governor *t;
3bcb09a3 381
3fc54d37 382 mutex_lock(&cpufreq_governor_mutex);
3bcb09a3
JF
383
384 t = __find_governor(str_governor);
385
ea714970 386 if (t == NULL) {
1a8e1463 387 int ret;
ea714970 388
1a8e1463
KC
389 mutex_unlock(&cpufreq_governor_mutex);
390 ret = request_module("cpufreq_%s", str_governor);
391 mutex_lock(&cpufreq_governor_mutex);
ea714970 392
1a8e1463
KC
393 if (ret == 0)
394 t = __find_governor(str_governor);
ea714970
JF
395 }
396
3bcb09a3
JF
397 if (t != NULL) {
398 *governor = t;
399 err = 0;
1da177e4 400 }
3bcb09a3 401
3fc54d37 402 mutex_unlock(&cpufreq_governor_mutex);
1da177e4 403 }
29464f28 404out:
3bcb09a3 405 return err;
1da177e4 406}
1da177e4 407
1da177e4 408/**
e08f5f5b
GS
409 * cpufreq_per_cpu_attr_read() / show_##file_name() -
410 * print out cpufreq information
1da177e4
LT
411 *
412 * Write out information from cpufreq_driver->policy[cpu]; object must be
413 * "unsigned int".
414 */
415
32ee8c3e
DJ
416#define show_one(file_name, object) \
417static ssize_t show_##file_name \
905d77cd 418(struct cpufreq_policy *policy, char *buf) \
32ee8c3e 419{ \
29464f28 420 return sprintf(buf, "%u\n", policy->object); \
1da177e4
LT
421}
422
423show_one(cpuinfo_min_freq, cpuinfo.min_freq);
424show_one(cpuinfo_max_freq, cpuinfo.max_freq);
ed129784 425show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
1da177e4
LT
426show_one(scaling_min_freq, min);
427show_one(scaling_max_freq, max);
428show_one(scaling_cur_freq, cur);
429
3a3e9e06
VK
430static int __cpufreq_set_policy(struct cpufreq_policy *policy,
431 struct cpufreq_policy *new_policy);
7970e08b 432
1da177e4
LT
433/**
434 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
435 */
436#define store_one(file_name, object) \
437static ssize_t store_##file_name \
905d77cd 438(struct cpufreq_policy *policy, const char *buf, size_t count) \
1da177e4 439{ \
f55c9c26 440 unsigned int ret; \
1da177e4
LT
441 struct cpufreq_policy new_policy; \
442 \
443 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
444 if (ret) \
445 return -EINVAL; \
446 \
29464f28 447 ret = sscanf(buf, "%u", &new_policy.object); \
1da177e4
LT
448 if (ret != 1) \
449 return -EINVAL; \
450 \
7970e08b
TR
451 ret = __cpufreq_set_policy(policy, &new_policy); \
452 policy->user_policy.object = policy->object; \
1da177e4
LT
453 \
454 return ret ? ret : count; \
455}
456
29464f28
DJ
457store_one(scaling_min_freq, min);
458store_one(scaling_max_freq, max);
1da177e4
LT
459
460/**
461 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
462 */
905d77cd
DJ
463static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
464 char *buf)
1da177e4 465{
5a01f2e8 466 unsigned int cur_freq = __cpufreq_get(policy->cpu);
1da177e4
LT
467 if (!cur_freq)
468 return sprintf(buf, "<unknown>");
469 return sprintf(buf, "%u\n", cur_freq);
470}
471
1da177e4
LT
472/**
473 * show_scaling_governor - show the current policy for the specified CPU
474 */
905d77cd 475static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
1da177e4 476{
29464f28 477 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
1da177e4
LT
478 return sprintf(buf, "powersave\n");
479 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
480 return sprintf(buf, "performance\n");
481 else if (policy->governor)
4b972f0b 482 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
29464f28 483 policy->governor->name);
1da177e4
LT
484 return -EINVAL;
485}
486
1da177e4
LT
487/**
488 * store_scaling_governor - store policy for the specified CPU
489 */
905d77cd
DJ
490static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
491 const char *buf, size_t count)
1da177e4 492{
f55c9c26 493 unsigned int ret;
1da177e4
LT
494 char str_governor[16];
495 struct cpufreq_policy new_policy;
496
497 ret = cpufreq_get_policy(&new_policy, policy->cpu);
498 if (ret)
499 return ret;
500
29464f28 501 ret = sscanf(buf, "%15s", str_governor);
1da177e4
LT
502 if (ret != 1)
503 return -EINVAL;
504
e08f5f5b
GS
505 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
506 &new_policy.governor))
1da177e4
LT
507 return -EINVAL;
508
bb176f7d
VK
509 /*
510 * Do not use cpufreq_set_policy here or the user_policy.max
511 * will be wrongly overridden
512 */
7970e08b
TR
513 ret = __cpufreq_set_policy(policy, &new_policy);
514
515 policy->user_policy.policy = policy->policy;
516 policy->user_policy.governor = policy->governor;
7970e08b 517
e08f5f5b
GS
518 if (ret)
519 return ret;
520 else
521 return count;
1da177e4
LT
522}
523
524/**
525 * show_scaling_driver - show the cpufreq driver currently loaded
526 */
905d77cd 527static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
1da177e4 528{
1c3d85dd 529 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
1da177e4
LT
530}
531
532/**
533 * show_scaling_available_governors - show the available CPUfreq governors
534 */
905d77cd
DJ
535static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
536 char *buf)
1da177e4
LT
537{
538 ssize_t i = 0;
539 struct cpufreq_governor *t;
540
1c3d85dd 541 if (!cpufreq_driver->target) {
1da177e4
LT
542 i += sprintf(buf, "performance powersave");
543 goto out;
544 }
545
546 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
29464f28
DJ
547 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
548 - (CPUFREQ_NAME_LEN + 2)))
1da177e4 549 goto out;
4b972f0b 550 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
1da177e4 551 }
7d5e350f 552out:
1da177e4
LT
553 i += sprintf(&buf[i], "\n");
554 return i;
555}
e8628dd0 556
f4fd3797 557ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
1da177e4
LT
558{
559 ssize_t i = 0;
560 unsigned int cpu;
561
835481d9 562 for_each_cpu(cpu, mask) {
1da177e4
LT
563 if (i)
564 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
565 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
566 if (i >= (PAGE_SIZE - 5))
29464f28 567 break;
1da177e4
LT
568 }
569 i += sprintf(&buf[i], "\n");
570 return i;
571}
f4fd3797 572EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
1da177e4 573
e8628dd0
DW
574/**
575 * show_related_cpus - show the CPUs affected by each transition even if
576 * hw coordination is in use
577 */
578static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
579{
f4fd3797 580 return cpufreq_show_cpus(policy->related_cpus, buf);
e8628dd0
DW
581}
582
583/**
584 * show_affected_cpus - show the CPUs affected by each transition
585 */
586static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
587{
f4fd3797 588 return cpufreq_show_cpus(policy->cpus, buf);
e8628dd0
DW
589}
590
9e76988e 591static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
905d77cd 592 const char *buf, size_t count)
9e76988e
VP
593{
594 unsigned int freq = 0;
595 unsigned int ret;
596
879000f9 597 if (!policy->governor || !policy->governor->store_setspeed)
9e76988e
VP
598 return -EINVAL;
599
600 ret = sscanf(buf, "%u", &freq);
601 if (ret != 1)
602 return -EINVAL;
603
604 policy->governor->store_setspeed(policy, freq);
605
606 return count;
607}
608
609static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
610{
879000f9 611 if (!policy->governor || !policy->governor->show_setspeed)
9e76988e
VP
612 return sprintf(buf, "<unsupported>\n");
613
614 return policy->governor->show_setspeed(policy, buf);
615}
1da177e4 616
e2f74f35 617/**
8bf1ac72 618 * show_bios_limit - show the current cpufreq HW/BIOS limitation
e2f74f35
TR
619 */
620static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
621{
622 unsigned int limit;
623 int ret;
1c3d85dd
RW
624 if (cpufreq_driver->bios_limit) {
625 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
e2f74f35
TR
626 if (!ret)
627 return sprintf(buf, "%u\n", limit);
628 }
629 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
630}
631
6dad2a29
BP
632cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
633cpufreq_freq_attr_ro(cpuinfo_min_freq);
634cpufreq_freq_attr_ro(cpuinfo_max_freq);
635cpufreq_freq_attr_ro(cpuinfo_transition_latency);
636cpufreq_freq_attr_ro(scaling_available_governors);
637cpufreq_freq_attr_ro(scaling_driver);
638cpufreq_freq_attr_ro(scaling_cur_freq);
639cpufreq_freq_attr_ro(bios_limit);
640cpufreq_freq_attr_ro(related_cpus);
641cpufreq_freq_attr_ro(affected_cpus);
642cpufreq_freq_attr_rw(scaling_min_freq);
643cpufreq_freq_attr_rw(scaling_max_freq);
644cpufreq_freq_attr_rw(scaling_governor);
645cpufreq_freq_attr_rw(scaling_setspeed);
1da177e4 646
905d77cd 647static struct attribute *default_attrs[] = {
1da177e4
LT
648 &cpuinfo_min_freq.attr,
649 &cpuinfo_max_freq.attr,
ed129784 650 &cpuinfo_transition_latency.attr,
1da177e4
LT
651 &scaling_min_freq.attr,
652 &scaling_max_freq.attr,
653 &affected_cpus.attr,
e8628dd0 654 &related_cpus.attr,
1da177e4
LT
655 &scaling_governor.attr,
656 &scaling_driver.attr,
657 &scaling_available_governors.attr,
9e76988e 658 &scaling_setspeed.attr,
1da177e4
LT
659 NULL
660};
661
29464f28
DJ
662#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
663#define to_attr(a) container_of(a, struct freq_attr, attr)
1da177e4 664
29464f28 665static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
1da177e4 666{
905d77cd
DJ
667 struct cpufreq_policy *policy = to_policy(kobj);
668 struct freq_attr *fattr = to_attr(attr);
0db4a8a9 669 ssize_t ret = -EINVAL;
6eed9404
VK
670
671 if (!down_read_trylock(&cpufreq_rwsem))
672 goto exit;
5a01f2e8
VP
673
674 if (lock_policy_rwsem_read(policy->cpu) < 0)
6eed9404 675 goto up_read;
5a01f2e8 676
e08f5f5b
GS
677 if (fattr->show)
678 ret = fattr->show(policy, buf);
679 else
680 ret = -EIO;
681
5a01f2e8 682 unlock_policy_rwsem_read(policy->cpu);
6eed9404
VK
683
684up_read:
685 up_read(&cpufreq_rwsem);
686exit:
1da177e4
LT
687 return ret;
688}
689
905d77cd
DJ
690static ssize_t store(struct kobject *kobj, struct attribute *attr,
691 const char *buf, size_t count)
1da177e4 692{
905d77cd
DJ
693 struct cpufreq_policy *policy = to_policy(kobj);
694 struct freq_attr *fattr = to_attr(attr);
a07530b4 695 ssize_t ret = -EINVAL;
6eed9404
VK
696
697 if (!down_read_trylock(&cpufreq_rwsem))
698 goto exit;
5a01f2e8
VP
699
700 if (lock_policy_rwsem_write(policy->cpu) < 0)
6eed9404 701 goto up_read;
5a01f2e8 702
e08f5f5b
GS
703 if (fattr->store)
704 ret = fattr->store(policy, buf, count);
705 else
706 ret = -EIO;
707
5a01f2e8 708 unlock_policy_rwsem_write(policy->cpu);
6eed9404
VK
709
710up_read:
711 up_read(&cpufreq_rwsem);
712exit:
1da177e4
LT
713 return ret;
714}
715
905d77cd 716static void cpufreq_sysfs_release(struct kobject *kobj)
1da177e4 717{
905d77cd 718 struct cpufreq_policy *policy = to_policy(kobj);
2d06d8c4 719 pr_debug("last reference is dropped\n");
1da177e4
LT
720 complete(&policy->kobj_unregister);
721}
722
52cf25d0 723static const struct sysfs_ops sysfs_ops = {
1da177e4
LT
724 .show = show,
725 .store = store,
726};
727
728static struct kobj_type ktype_cpufreq = {
729 .sysfs_ops = &sysfs_ops,
730 .default_attrs = default_attrs,
731 .release = cpufreq_sysfs_release,
732};
733
2361be23
VK
734struct kobject *cpufreq_global_kobject;
735EXPORT_SYMBOL(cpufreq_global_kobject);
736
737static int cpufreq_global_kobject_usage;
738
739int cpufreq_get_global_kobject(void)
740{
741 if (!cpufreq_global_kobject_usage++)
742 return kobject_add(cpufreq_global_kobject,
743 &cpu_subsys.dev_root->kobj, "%s", "cpufreq");
744
745 return 0;
746}
747EXPORT_SYMBOL(cpufreq_get_global_kobject);
748
749void cpufreq_put_global_kobject(void)
750{
751 if (!--cpufreq_global_kobject_usage)
752 kobject_del(cpufreq_global_kobject);
753}
754EXPORT_SYMBOL(cpufreq_put_global_kobject);
755
756int cpufreq_sysfs_create_file(const struct attribute *attr)
757{
758 int ret = cpufreq_get_global_kobject();
759
760 if (!ret) {
761 ret = sysfs_create_file(cpufreq_global_kobject, attr);
762 if (ret)
763 cpufreq_put_global_kobject();
764 }
765
766 return ret;
767}
768EXPORT_SYMBOL(cpufreq_sysfs_create_file);
769
770void cpufreq_sysfs_remove_file(const struct attribute *attr)
771{
772 sysfs_remove_file(cpufreq_global_kobject, attr);
773 cpufreq_put_global_kobject();
774}
775EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
776
19d6f7ec 777/* symlink affected CPUs */
308b60e7 778static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
19d6f7ec
DJ
779{
780 unsigned int j;
781 int ret = 0;
782
783 for_each_cpu(j, policy->cpus) {
8a25a2fd 784 struct device *cpu_dev;
19d6f7ec 785
308b60e7 786 if (j == policy->cpu)
19d6f7ec 787 continue;
19d6f7ec 788
e8fdde10 789 pr_debug("Adding link for CPU: %u\n", j);
8a25a2fd
KS
790 cpu_dev = get_cpu_device(j);
791 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
19d6f7ec 792 "cpufreq");
71c3461e
RW
793 if (ret)
794 break;
19d6f7ec
DJ
795 }
796 return ret;
797}
798
308b60e7 799static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
8a25a2fd 800 struct device *dev)
909a694e
DJ
801{
802 struct freq_attr **drv_attr;
909a694e 803 int ret = 0;
909a694e
DJ
804
805 /* prepare interface data */
806 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
8a25a2fd 807 &dev->kobj, "cpufreq");
909a694e
DJ
808 if (ret)
809 return ret;
810
811 /* set up files for this cpu device */
1c3d85dd 812 drv_attr = cpufreq_driver->attr;
909a694e
DJ
813 while ((drv_attr) && (*drv_attr)) {
814 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
815 if (ret)
1c3d85dd 816 goto err_out_kobj_put;
909a694e
DJ
817 drv_attr++;
818 }
1c3d85dd 819 if (cpufreq_driver->get) {
909a694e
DJ
820 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
821 if (ret)
1c3d85dd 822 goto err_out_kobj_put;
909a694e 823 }
1c3d85dd 824 if (cpufreq_driver->target) {
909a694e
DJ
825 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
826 if (ret)
1c3d85dd 827 goto err_out_kobj_put;
909a694e 828 }
1c3d85dd 829 if (cpufreq_driver->bios_limit) {
e2f74f35
TR
830 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
831 if (ret)
1c3d85dd 832 goto err_out_kobj_put;
e2f74f35 833 }
909a694e 834
308b60e7 835 ret = cpufreq_add_dev_symlink(policy);
ecf7e461
DJ
836 if (ret)
837 goto err_out_kobj_put;
838
e18f1682
SB
839 return ret;
840
841err_out_kobj_put:
842 kobject_put(&policy->kobj);
843 wait_for_completion(&policy->kobj_unregister);
844 return ret;
845}
846
847static void cpufreq_init_policy(struct cpufreq_policy *policy)
848{
849 struct cpufreq_policy new_policy;
850 int ret = 0;
851
d5b73cd8 852 memcpy(&new_policy, policy, sizeof(*policy));
ecf7e461
DJ
853 /* assure that the starting sequence is run in __cpufreq_set_policy */
854 policy->governor = NULL;
855
856 /* set default policy */
857 ret = __cpufreq_set_policy(policy, &new_policy);
858 policy->user_policy.policy = policy->policy;
859 policy->user_policy.governor = policy->governor;
860
861 if (ret) {
2d06d8c4 862 pr_debug("setting policy failed\n");
1c3d85dd
RW
863 if (cpufreq_driver->exit)
864 cpufreq_driver->exit(policy);
ecf7e461 865 }
909a694e
DJ
866}
867
fcf80582 868#ifdef CONFIG_HOTPLUG_CPU
d8d3b471
VK
869static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,
870 unsigned int cpu, struct device *dev,
871 bool frozen)
fcf80582 872{
1c3d85dd 873 int ret = 0, has_target = !!cpufreq_driver->target;
fcf80582
VK
874 unsigned long flags;
875
3de9bdeb
VK
876 if (has_target) {
877 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
878 if (ret) {
879 pr_err("%s: Failed to stop governor\n", __func__);
880 return ret;
881 }
882 }
fcf80582 883
d8d3b471 884 lock_policy_rwsem_write(policy->cpu);
2eaa3e2d 885
0d1857a1 886 write_lock_irqsave(&cpufreq_driver_lock, flags);
2eaa3e2d 887
fcf80582
VK
888 cpumask_set_cpu(cpu, policy->cpus);
889 per_cpu(cpufreq_cpu_data, cpu) = policy;
0d1857a1 890 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
fcf80582 891
d8d3b471 892 unlock_policy_rwsem_write(policy->cpu);
2eaa3e2d 893
820c6ca2 894 if (has_target) {
3de9bdeb
VK
895 if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
896 (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
897 pr_err("%s: Failed to start governor\n", __func__);
898 return ret;
899 }
820c6ca2 900 }
fcf80582 901
a82fab29 902 /* Don't touch sysfs links during light-weight init */
71c3461e
RW
903 if (!frozen)
904 ret = sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
a82fab29
SB
905
906 return ret;
fcf80582
VK
907}
908#endif
1da177e4 909
8414809c
SB
910static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu)
911{
912 struct cpufreq_policy *policy;
913 unsigned long flags;
914
915 write_lock_irqsave(&cpufreq_driver_lock, flags);
916
917 policy = per_cpu(cpufreq_cpu_data_fallback, cpu);
918
919 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
920
921 return policy;
922}
923
e9698cc5
SB
924static struct cpufreq_policy *cpufreq_policy_alloc(void)
925{
926 struct cpufreq_policy *policy;
927
928 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
929 if (!policy)
930 return NULL;
931
932 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
933 goto err_free_policy;
934
935 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
936 goto err_free_cpumask;
937
c88a1f8b 938 INIT_LIST_HEAD(&policy->policy_list);
e9698cc5
SB
939 return policy;
940
941err_free_cpumask:
942 free_cpumask_var(policy->cpus);
943err_free_policy:
944 kfree(policy);
945
946 return NULL;
947}
948
949static void cpufreq_policy_free(struct cpufreq_policy *policy)
950{
951 free_cpumask_var(policy->related_cpus);
952 free_cpumask_var(policy->cpus);
953 kfree(policy);
954}
955
a82fab29
SB
956static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif,
957 bool frozen)
1da177e4 958{
fcf80582 959 unsigned int j, cpu = dev->id;
65922465 960 int ret = -ENOMEM;
1da177e4 961 struct cpufreq_policy *policy;
1da177e4 962 unsigned long flags;
90e41bac 963#ifdef CONFIG_HOTPLUG_CPU
1b274294 964 struct cpufreq_policy *tpolicy;
fcf80582 965 struct cpufreq_governor *gov;
90e41bac 966#endif
1da177e4 967
c32b6b8e
AR
968 if (cpu_is_offline(cpu))
969 return 0;
970
2d06d8c4 971 pr_debug("adding CPU %u\n", cpu);
1da177e4
LT
972
973#ifdef CONFIG_SMP
974 /* check whether a different CPU already registered this
975 * CPU because it is in the same boat. */
976 policy = cpufreq_cpu_get(cpu);
977 if (unlikely(policy)) {
8ff69732 978 cpufreq_cpu_put(policy);
1da177e4
LT
979 return 0;
980 }
5025d628 981#endif
fcf80582 982
6eed9404
VK
983 if (!down_read_trylock(&cpufreq_rwsem))
984 return 0;
985
fcf80582
VK
986#ifdef CONFIG_HOTPLUG_CPU
987 /* Check if this cpu was hot-unplugged earlier and has siblings */
0d1857a1 988 read_lock_irqsave(&cpufreq_driver_lock, flags);
1b274294
VK
989 list_for_each_entry(tpolicy, &cpufreq_policy_list, policy_list) {
990 if (cpumask_test_cpu(cpu, tpolicy->related_cpus)) {
0d1857a1 991 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1b274294 992 ret = cpufreq_add_policy_cpu(tpolicy, cpu, dev, frozen);
6eed9404
VK
993 up_read(&cpufreq_rwsem);
994 return ret;
2eaa3e2d 995 }
fcf80582 996 }
0d1857a1 997 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1da177e4
LT
998#endif
999
8414809c
SB
1000 if (frozen)
1001 /* Restore the saved policy when doing light-weight init */
1002 policy = cpufreq_policy_restore(cpu);
1003 else
1004 policy = cpufreq_policy_alloc();
1005
059019a3 1006 if (!policy)
1da177e4 1007 goto nomem_out;
059019a3 1008
1da177e4 1009 policy->cpu = cpu;
65922465 1010 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
835481d9 1011 cpumask_copy(policy->cpus, cpumask_of(cpu));
1da177e4 1012
1da177e4 1013 init_completion(&policy->kobj_unregister);
65f27f38 1014 INIT_WORK(&policy->update, handle_update);
1da177e4
LT
1015
1016 /* call driver. From then on the cpufreq must be able
1017 * to accept all calls to ->verify and ->setpolicy for this CPU
1018 */
1c3d85dd 1019 ret = cpufreq_driver->init(policy);
1da177e4 1020 if (ret) {
2d06d8c4 1021 pr_debug("initialization failed\n");
2eaa3e2d 1022 goto err_set_policy_cpu;
1da177e4 1023 }
643ae6e8 1024
fcf80582
VK
1025 /* related cpus should atleast have policy->cpus */
1026 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
1027
643ae6e8
VK
1028 /*
1029 * affected cpus must always be the one, which are online. We aren't
1030 * managing offline cpus here.
1031 */
1032 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1033
187d9f4e
MC
1034 policy->user_policy.min = policy->min;
1035 policy->user_policy.max = policy->max;
1da177e4 1036
a1531acd
TR
1037 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1038 CPUFREQ_START, policy);
1039
fcf80582
VK
1040#ifdef CONFIG_HOTPLUG_CPU
1041 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
1042 if (gov) {
1043 policy->governor = gov;
1044 pr_debug("Restoring governor %s for cpu %d\n",
1045 policy->governor->name, cpu);
4bfa042c 1046 }
fcf80582 1047#endif
1da177e4 1048
e18f1682 1049 write_lock_irqsave(&cpufreq_driver_lock, flags);
474deff7 1050 for_each_cpu(j, policy->cpus)
e18f1682 1051 per_cpu(cpufreq_cpu_data, j) = policy;
e18f1682
SB
1052 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1053
a82fab29 1054 if (!frozen) {
308b60e7 1055 ret = cpufreq_add_dev_interface(policy, dev);
a82fab29
SB
1056 if (ret)
1057 goto err_out_unregister;
1058 }
8ff69732 1059
9515f4d6
VK
1060 write_lock_irqsave(&cpufreq_driver_lock, flags);
1061 list_add(&policy->policy_list, &cpufreq_policy_list);
1062 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1063
e18f1682
SB
1064 cpufreq_init_policy(policy);
1065
038c5b3e 1066 kobject_uevent(&policy->kobj, KOBJ_ADD);
6eed9404
VK
1067 up_read(&cpufreq_rwsem);
1068
2d06d8c4 1069 pr_debug("initialization complete\n");
87c32271 1070
1da177e4
LT
1071 return 0;
1072
1da177e4 1073err_out_unregister:
0d1857a1 1074 write_lock_irqsave(&cpufreq_driver_lock, flags);
474deff7 1075 for_each_cpu(j, policy->cpus)
7a6aedfa 1076 per_cpu(cpufreq_cpu_data, j) = NULL;
0d1857a1 1077 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1da177e4 1078
2eaa3e2d 1079err_set_policy_cpu:
e9698cc5 1080 cpufreq_policy_free(policy);
1da177e4 1081nomem_out:
6eed9404
VK
1082 up_read(&cpufreq_rwsem);
1083
1da177e4
LT
1084 return ret;
1085}
1086
a82fab29
SB
1087/**
1088 * cpufreq_add_dev - add a CPU device
1089 *
1090 * Adds the cpufreq interface for a CPU device.
1091 *
1092 * The Oracle says: try running cpufreq registration/unregistration concurrently
1093 * with with cpu hotplugging and all hell will break loose. Tried to clean this
1094 * mess up, but more thorough testing is needed. - Mathieu
1095 */
1096static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1097{
1098 return __cpufreq_add_dev(dev, sif, false);
1099}
1100
b8eed8af
VK
1101static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
1102{
b8eed8af
VK
1103 policy->last_cpu = policy->cpu;
1104 policy->cpu = cpu;
1105
b8eed8af
VK
1106#ifdef CONFIG_CPU_FREQ_TABLE
1107 cpufreq_frequency_table_update_policy_cpu(policy);
1108#endif
1109 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1110 CPUFREQ_UPDATE_POLICY_CPU, policy);
1111}
1da177e4 1112
3a3e9e06 1113static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy *policy,
a82fab29 1114 unsigned int old_cpu, bool frozen)
f9ba680d
SB
1115{
1116 struct device *cpu_dev;
f9ba680d
SB
1117 int ret;
1118
1119 /* first sibling now owns the new sysfs dir */
3a3e9e06 1120 cpu_dev = get_cpu_device(cpumask_first(policy->cpus));
a82fab29
SB
1121
1122 /* Don't touch sysfs files during light-weight tear-down */
1123 if (frozen)
1124 return cpu_dev->id;
1125
f9ba680d 1126 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
3a3e9e06 1127 ret = kobject_move(&policy->kobj, &cpu_dev->kobj);
f9ba680d
SB
1128 if (ret) {
1129 pr_err("%s: Failed to move kobj: %d", __func__, ret);
1130
1131 WARN_ON(lock_policy_rwsem_write(old_cpu));
3a3e9e06 1132 cpumask_set_cpu(old_cpu, policy->cpus);
f9ba680d
SB
1133 unlock_policy_rwsem_write(old_cpu);
1134
3a3e9e06 1135 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
f9ba680d
SB
1136 "cpufreq");
1137
1138 return -EINVAL;
1139 }
1140
1141 return cpu_dev->id;
1142}
1143
1da177e4 1144/**
5a01f2e8 1145 * __cpufreq_remove_dev - remove a CPU device
1da177e4
LT
1146 *
1147 * Removes the cpufreq interface for a CPU device.
5a01f2e8
VP
1148 * Caller should already have policy_rwsem in write mode for this CPU.
1149 * This routine frees the rwsem before returning.
1da177e4 1150 */
bb176f7d 1151static int __cpufreq_remove_dev(struct device *dev,
a82fab29 1152 struct subsys_interface *sif, bool frozen)
1da177e4 1153{
f9ba680d 1154 unsigned int cpu = dev->id, cpus;
3de9bdeb 1155 int new_cpu, ret;
1da177e4 1156 unsigned long flags;
3a3e9e06 1157 struct cpufreq_policy *policy;
499bca9b
AW
1158 struct kobject *kobj;
1159 struct completion *cmp;
1da177e4 1160
b8eed8af 1161 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
1da177e4 1162
0d1857a1 1163 write_lock_irqsave(&cpufreq_driver_lock, flags);
2eaa3e2d 1164
3a3e9e06 1165 policy = per_cpu(cpufreq_cpu_data, cpu);
2eaa3e2d 1166
8414809c
SB
1167 /* Save the policy somewhere when doing a light-weight tear-down */
1168 if (frozen)
3a3e9e06 1169 per_cpu(cpufreq_cpu_data_fallback, cpu) = policy;
8414809c 1170
0d1857a1 1171 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1da177e4 1172
3a3e9e06 1173 if (!policy) {
b8eed8af 1174 pr_debug("%s: No cpu_data found\n", __func__);
1da177e4
LT
1175 return -EINVAL;
1176 }
1da177e4 1177
3de9bdeb
VK
1178 if (cpufreq_driver->target) {
1179 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1180 if (ret) {
1181 pr_err("%s: Failed to stop governor\n", __func__);
1182 return ret;
1183 }
1184 }
1da177e4 1185
084f3493 1186#ifdef CONFIG_HOTPLUG_CPU
1c3d85dd 1187 if (!cpufreq_driver->setpolicy)
fa69e33f 1188 strncpy(per_cpu(cpufreq_cpu_governor, cpu),
3a3e9e06 1189 policy->governor->name, CPUFREQ_NAME_LEN);
1da177e4
LT
1190#endif
1191
2eaa3e2d 1192 WARN_ON(lock_policy_rwsem_write(cpu));
3a3e9e06 1193 cpus = cpumask_weight(policy->cpus);
e4969eba
VK
1194
1195 if (cpus > 1)
3a3e9e06 1196 cpumask_clear_cpu(cpu, policy->cpus);
2eaa3e2d 1197 unlock_policy_rwsem_write(cpu);
084f3493 1198
3a3e9e06 1199 if (cpu != policy->cpu && !frozen) {
73bf0fc2
VK
1200 sysfs_remove_link(&dev->kobj, "cpufreq");
1201 } else if (cpus > 1) {
084f3493 1202
3a3e9e06 1203 new_cpu = cpufreq_nominate_new_policy_cpu(policy, cpu, frozen);
f9ba680d 1204 if (new_cpu >= 0) {
2eaa3e2d 1205 WARN_ON(lock_policy_rwsem_write(cpu));
3a3e9e06 1206 update_policy_cpu(policy, new_cpu);
499bca9b 1207 unlock_policy_rwsem_write(cpu);
a82fab29
SB
1208
1209 if (!frozen) {
1210 pr_debug("%s: policy Kobject moved to cpu: %d "
1211 "from: %d\n",__func__, new_cpu, cpu);
1212 }
1da177e4
LT
1213 }
1214 }
1da177e4 1215
b8eed8af
VK
1216 /* If cpu is last user of policy, free policy */
1217 if (cpus == 1) {
3de9bdeb
VK
1218 if (cpufreq_driver->target) {
1219 ret = __cpufreq_governor(policy,
1220 CPUFREQ_GOV_POLICY_EXIT);
1221 if (ret) {
1222 pr_err("%s: Failed to exit governor\n",
1223 __func__);
1224 return ret;
1225 }
edab2fbc 1226 }
2a998599 1227
8414809c
SB
1228 if (!frozen) {
1229 lock_policy_rwsem_read(cpu);
3a3e9e06
VK
1230 kobj = &policy->kobj;
1231 cmp = &policy->kobj_unregister;
8414809c
SB
1232 unlock_policy_rwsem_read(cpu);
1233 kobject_put(kobj);
1234
1235 /*
1236 * We need to make sure that the underlying kobj is
1237 * actually not referenced anymore by anybody before we
1238 * proceed with unloading.
1239 */
1240 pr_debug("waiting for dropping of refcount\n");
1241 wait_for_completion(cmp);
1242 pr_debug("wait complete\n");
1243 }
7d26e2d5 1244
8414809c
SB
1245 /*
1246 * Perform the ->exit() even during light-weight tear-down,
1247 * since this is a core component, and is essential for the
1248 * subsequent light-weight ->init() to succeed.
b8eed8af 1249 */
1c3d85dd 1250 if (cpufreq_driver->exit)
3a3e9e06 1251 cpufreq_driver->exit(policy);
27ecddc2 1252
9515f4d6
VK
1253 /* Remove policy from list of active policies */
1254 write_lock_irqsave(&cpufreq_driver_lock, flags);
1255 list_del(&policy->policy_list);
1256 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1257
8414809c 1258 if (!frozen)
3a3e9e06 1259 cpufreq_policy_free(policy);
2a998599 1260 } else {
2a998599 1261 if (cpufreq_driver->target) {
3de9bdeb
VK
1262 if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
1263 (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
1264 pr_err("%s: Failed to start governor\n",
1265 __func__);
1266 return ret;
1267 }
2a998599 1268 }
27ecddc2 1269 }
1da177e4 1270
474deff7 1271 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1da177e4
LT
1272 return 0;
1273}
1274
8a25a2fd 1275static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
5a01f2e8 1276{
8a25a2fd 1277 unsigned int cpu = dev->id;
5a01f2e8 1278 int retval;
ec28297a
VP
1279
1280 if (cpu_is_offline(cpu))
1281 return 0;
1282
a82fab29 1283 retval = __cpufreq_remove_dev(dev, sif, false);
5a01f2e8
VP
1284 return retval;
1285}
1286
65f27f38 1287static void handle_update(struct work_struct *work)
1da177e4 1288{
65f27f38
DH
1289 struct cpufreq_policy *policy =
1290 container_of(work, struct cpufreq_policy, update);
1291 unsigned int cpu = policy->cpu;
2d06d8c4 1292 pr_debug("handle_update for cpu %u called\n", cpu);
1da177e4
LT
1293 cpufreq_update_policy(cpu);
1294}
1295
1296/**
bb176f7d
VK
1297 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1298 * in deep trouble.
1da177e4
LT
1299 * @cpu: cpu number
1300 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1301 * @new_freq: CPU frequency the CPU actually runs at
1302 *
29464f28
DJ
1303 * We adjust to current frequency first, and need to clean up later.
1304 * So either call to cpufreq_update_policy() or schedule handle_update()).
1da177e4 1305 */
e08f5f5b
GS
1306static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1307 unsigned int new_freq)
1da177e4 1308{
b43a7ffb 1309 struct cpufreq_policy *policy;
1da177e4 1310 struct cpufreq_freqs freqs;
b43a7ffb
VK
1311 unsigned long flags;
1312
2d06d8c4 1313 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
1da177e4
LT
1314 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1315
1da177e4
LT
1316 freqs.old = old_freq;
1317 freqs.new = new_freq;
b43a7ffb
VK
1318
1319 read_lock_irqsave(&cpufreq_driver_lock, flags);
1320 policy = per_cpu(cpufreq_cpu_data, cpu);
1321 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1322
1323 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1324 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
1da177e4
LT
1325}
1326
32ee8c3e 1327/**
4ab70df4 1328 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
95235ca2
VP
1329 * @cpu: CPU number
1330 *
1331 * This is the last known freq, without actually getting it from the driver.
1332 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1333 */
1334unsigned int cpufreq_quick_get(unsigned int cpu)
1335{
9e21ba8b 1336 struct cpufreq_policy *policy;
e08f5f5b 1337 unsigned int ret_freq = 0;
95235ca2 1338
1c3d85dd
RW
1339 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1340 return cpufreq_driver->get(cpu);
9e21ba8b
DB
1341
1342 policy = cpufreq_cpu_get(cpu);
95235ca2 1343 if (policy) {
e08f5f5b 1344 ret_freq = policy->cur;
95235ca2
VP
1345 cpufreq_cpu_put(policy);
1346 }
1347
4d34a67d 1348 return ret_freq;
95235ca2
VP
1349}
1350EXPORT_SYMBOL(cpufreq_quick_get);
1351
3d737108
JB
1352/**
1353 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1354 * @cpu: CPU number
1355 *
1356 * Just return the max possible frequency for a given CPU.
1357 */
1358unsigned int cpufreq_quick_get_max(unsigned int cpu)
1359{
1360 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1361 unsigned int ret_freq = 0;
1362
1363 if (policy) {
1364 ret_freq = policy->max;
1365 cpufreq_cpu_put(policy);
1366 }
1367
1368 return ret_freq;
1369}
1370EXPORT_SYMBOL(cpufreq_quick_get_max);
1371
5a01f2e8 1372static unsigned int __cpufreq_get(unsigned int cpu)
1da177e4 1373{
7a6aedfa 1374 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
e08f5f5b 1375 unsigned int ret_freq = 0;
5800043b 1376
1c3d85dd 1377 if (!cpufreq_driver->get)
4d34a67d 1378 return ret_freq;
1da177e4 1379
1c3d85dd 1380 ret_freq = cpufreq_driver->get(cpu);
1da177e4 1381
e08f5f5b 1382 if (ret_freq && policy->cur &&
1c3d85dd 1383 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
e08f5f5b
GS
1384 /* verify no discrepancy between actual and
1385 saved value exists */
1386 if (unlikely(ret_freq != policy->cur)) {
1387 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
1da177e4
LT
1388 schedule_work(&policy->update);
1389 }
1390 }
1391
4d34a67d 1392 return ret_freq;
5a01f2e8 1393}
1da177e4 1394
5a01f2e8
VP
1395/**
1396 * cpufreq_get - get the current CPU frequency (in kHz)
1397 * @cpu: CPU number
1398 *
1399 * Get the CPU current (static) CPU frequency
1400 */
1401unsigned int cpufreq_get(unsigned int cpu)
1402{
1403 unsigned int ret_freq = 0;
5a01f2e8 1404
6eed9404
VK
1405 if (!down_read_trylock(&cpufreq_rwsem))
1406 return 0;
5a01f2e8
VP
1407
1408 if (unlikely(lock_policy_rwsem_read(cpu)))
1409 goto out_policy;
1410
1411 ret_freq = __cpufreq_get(cpu);
1412
1413 unlock_policy_rwsem_read(cpu);
1da177e4 1414
5a01f2e8 1415out_policy:
6eed9404
VK
1416 up_read(&cpufreq_rwsem);
1417
4d34a67d 1418 return ret_freq;
1da177e4
LT
1419}
1420EXPORT_SYMBOL(cpufreq_get);
1421
8a25a2fd
KS
1422static struct subsys_interface cpufreq_interface = {
1423 .name = "cpufreq",
1424 .subsys = &cpu_subsys,
1425 .add_dev = cpufreq_add_dev,
1426 .remove_dev = cpufreq_remove_dev,
e00e56df
RW
1427};
1428
42d4dc3f 1429/**
e00e56df
RW
1430 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1431 *
1432 * This function is only executed for the boot processor. The other CPUs
1433 * have been put offline by means of CPU hotplug.
42d4dc3f 1434 */
e00e56df 1435static int cpufreq_bp_suspend(void)
42d4dc3f 1436{
e08f5f5b 1437 int ret = 0;
4bc5d341 1438
e00e56df 1439 int cpu = smp_processor_id();
3a3e9e06 1440 struct cpufreq_policy *policy;
42d4dc3f 1441
2d06d8c4 1442 pr_debug("suspending cpu %u\n", cpu);
42d4dc3f 1443
e00e56df 1444 /* If there's no policy for the boot CPU, we have nothing to do. */
3a3e9e06
VK
1445 policy = cpufreq_cpu_get(cpu);
1446 if (!policy)
e00e56df 1447 return 0;
42d4dc3f 1448
1c3d85dd 1449 if (cpufreq_driver->suspend) {
3a3e9e06 1450 ret = cpufreq_driver->suspend(policy);
ce6c3997 1451 if (ret)
42d4dc3f 1452 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
3a3e9e06 1453 "step on CPU %u\n", policy->cpu);
42d4dc3f
BH
1454 }
1455
3a3e9e06 1456 cpufreq_cpu_put(policy);
c9060494 1457 return ret;
42d4dc3f
BH
1458}
1459
1da177e4 1460/**
e00e56df 1461 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
1da177e4
LT
1462 *
1463 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
ce6c3997
DB
1464 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1465 * restored. It will verify that the current freq is in sync with
1466 * what we believe it to be. This is a bit later than when it
1467 * should be, but nonethteless it's better than calling
1468 * cpufreq_driver->get() here which might re-enable interrupts...
e00e56df
RW
1469 *
1470 * This function is only executed for the boot CPU. The other CPUs have not
1471 * been turned on yet.
1da177e4 1472 */
e00e56df 1473static void cpufreq_bp_resume(void)
1da177e4 1474{
e08f5f5b 1475 int ret = 0;
4bc5d341 1476
e00e56df 1477 int cpu = smp_processor_id();
3a3e9e06 1478 struct cpufreq_policy *policy;
1da177e4 1479
2d06d8c4 1480 pr_debug("resuming cpu %u\n", cpu);
1da177e4 1481
e00e56df 1482 /* If there's no policy for the boot CPU, we have nothing to do. */
3a3e9e06
VK
1483 policy = cpufreq_cpu_get(cpu);
1484 if (!policy)
e00e56df 1485 return;
1da177e4 1486
1c3d85dd 1487 if (cpufreq_driver->resume) {
3a3e9e06 1488 ret = cpufreq_driver->resume(policy);
1da177e4
LT
1489 if (ret) {
1490 printk(KERN_ERR "cpufreq: resume failed in ->resume "
3a3e9e06 1491 "step on CPU %u\n", policy->cpu);
c9060494 1492 goto fail;
1da177e4
LT
1493 }
1494 }
1495
3a3e9e06 1496 schedule_work(&policy->update);
ce6c3997 1497
c9060494 1498fail:
3a3e9e06 1499 cpufreq_cpu_put(policy);
1da177e4
LT
1500}
1501
e00e56df
RW
1502static struct syscore_ops cpufreq_syscore_ops = {
1503 .suspend = cpufreq_bp_suspend,
1504 .resume = cpufreq_bp_resume,
1da177e4
LT
1505};
1506
9d95046e
BP
1507/**
1508 * cpufreq_get_current_driver - return current driver's name
1509 *
1510 * Return the name string of the currently loaded cpufreq driver
1511 * or NULL, if none.
1512 */
1513const char *cpufreq_get_current_driver(void)
1514{
1c3d85dd
RW
1515 if (cpufreq_driver)
1516 return cpufreq_driver->name;
1517
1518 return NULL;
9d95046e
BP
1519}
1520EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
1da177e4
LT
1521
1522/*********************************************************************
1523 * NOTIFIER LISTS INTERFACE *
1524 *********************************************************************/
1525
1526/**
1527 * cpufreq_register_notifier - register a driver with cpufreq
1528 * @nb: notifier function to register
1529 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1530 *
32ee8c3e 1531 * Add a driver to one of two lists: either a list of drivers that
1da177e4
LT
1532 * are notified about clock rate changes (once before and once after
1533 * the transition), or a list of drivers that are notified about
1534 * changes in cpufreq policy.
1535 *
1536 * This function may sleep, and has the same return conditions as
e041c683 1537 * blocking_notifier_chain_register.
1da177e4
LT
1538 */
1539int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1540{
1541 int ret;
1542
d5aaffa9
DB
1543 if (cpufreq_disabled())
1544 return -EINVAL;
1545
74212ca4
CEB
1546 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1547
1da177e4
LT
1548 switch (list) {
1549 case CPUFREQ_TRANSITION_NOTIFIER:
b4dfdbb3 1550 ret = srcu_notifier_chain_register(
e041c683 1551 &cpufreq_transition_notifier_list, nb);
1da177e4
LT
1552 break;
1553 case CPUFREQ_POLICY_NOTIFIER:
e041c683
AS
1554 ret = blocking_notifier_chain_register(
1555 &cpufreq_policy_notifier_list, nb);
1da177e4
LT
1556 break;
1557 default:
1558 ret = -EINVAL;
1559 }
1da177e4
LT
1560
1561 return ret;
1562}
1563EXPORT_SYMBOL(cpufreq_register_notifier);
1564
1da177e4
LT
1565/**
1566 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1567 * @nb: notifier block to be unregistered
bb176f7d 1568 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1da177e4
LT
1569 *
1570 * Remove a driver from the CPU frequency notifier list.
1571 *
1572 * This function may sleep, and has the same return conditions as
e041c683 1573 * blocking_notifier_chain_unregister.
1da177e4
LT
1574 */
1575int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1576{
1577 int ret;
1578
d5aaffa9
DB
1579 if (cpufreq_disabled())
1580 return -EINVAL;
1581
1da177e4
LT
1582 switch (list) {
1583 case CPUFREQ_TRANSITION_NOTIFIER:
b4dfdbb3 1584 ret = srcu_notifier_chain_unregister(
e041c683 1585 &cpufreq_transition_notifier_list, nb);
1da177e4
LT
1586 break;
1587 case CPUFREQ_POLICY_NOTIFIER:
e041c683
AS
1588 ret = blocking_notifier_chain_unregister(
1589 &cpufreq_policy_notifier_list, nb);
1da177e4
LT
1590 break;
1591 default:
1592 ret = -EINVAL;
1593 }
1da177e4
LT
1594
1595 return ret;
1596}
1597EXPORT_SYMBOL(cpufreq_unregister_notifier);
1598
1599
1600/*********************************************************************
1601 * GOVERNORS *
1602 *********************************************************************/
1603
1da177e4
LT
1604int __cpufreq_driver_target(struct cpufreq_policy *policy,
1605 unsigned int target_freq,
1606 unsigned int relation)
1607{
1608 int retval = -EINVAL;
7249924e 1609 unsigned int old_target_freq = target_freq;
c32b6b8e 1610
a7b422cd
KRW
1611 if (cpufreq_disabled())
1612 return -ENODEV;
7c30ed53
VK
1613 if (policy->transition_ongoing)
1614 return -EBUSY;
a7b422cd 1615
7249924e
VK
1616 /* Make sure that target_freq is within supported range */
1617 if (target_freq > policy->max)
1618 target_freq = policy->max;
1619 if (target_freq < policy->min)
1620 target_freq = policy->min;
1621
1622 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1623 policy->cpu, target_freq, relation, old_target_freq);
5a1c0228
VK
1624
1625 if (target_freq == policy->cur)
1626 return 0;
1627
1c3d85dd
RW
1628 if (cpufreq_driver->target)
1629 retval = cpufreq_driver->target(policy, target_freq, relation);
90d45d17 1630
1da177e4
LT
1631 return retval;
1632}
1633EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1634
1da177e4
LT
1635int cpufreq_driver_target(struct cpufreq_policy *policy,
1636 unsigned int target_freq,
1637 unsigned int relation)
1638{
f1829e4a 1639 int ret = -EINVAL;
1da177e4 1640
5a01f2e8 1641 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
f1829e4a 1642 goto fail;
1da177e4
LT
1643
1644 ret = __cpufreq_driver_target(policy, target_freq, relation);
1645
5a01f2e8 1646 unlock_policy_rwsem_write(policy->cpu);
1da177e4 1647
f1829e4a 1648fail:
1da177e4
LT
1649 return ret;
1650}
1651EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1652
153d7f3f 1653/*
153d7f3f
AV
1654 * when "event" is CPUFREQ_GOV_LIMITS
1655 */
1da177e4 1656
e08f5f5b
GS
1657static int __cpufreq_governor(struct cpufreq_policy *policy,
1658 unsigned int event)
1da177e4 1659{
cc993cab 1660 int ret;
6afde10c
TR
1661
1662 /* Only must be defined when default governor is known to have latency
1663 restrictions, like e.g. conservative or ondemand.
1664 That this is the case is already ensured in Kconfig
1665 */
1666#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1667 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1668#else
1669 struct cpufreq_governor *gov = NULL;
1670#endif
1c256245
TR
1671
1672 if (policy->governor->max_transition_latency &&
1673 policy->cpuinfo.transition_latency >
1674 policy->governor->max_transition_latency) {
6afde10c
TR
1675 if (!gov)
1676 return -EINVAL;
1677 else {
1678 printk(KERN_WARNING "%s governor failed, too long"
1679 " transition latency of HW, fallback"
1680 " to %s governor\n",
1681 policy->governor->name,
1682 gov->name);
1683 policy->governor = gov;
1684 }
1c256245 1685 }
1da177e4 1686
fe492f3f
VK
1687 if (event == CPUFREQ_GOV_POLICY_INIT)
1688 if (!try_module_get(policy->governor->owner))
1689 return -EINVAL;
1da177e4 1690
2d06d8c4 1691 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
e08f5f5b 1692 policy->cpu, event);
95731ebb
XC
1693
1694 mutex_lock(&cpufreq_governor_lock);
1695 if ((!policy->governor_enabled && (event == CPUFREQ_GOV_STOP)) ||
1696 (policy->governor_enabled && (event == CPUFREQ_GOV_START))) {
1697 mutex_unlock(&cpufreq_governor_lock);
1698 return -EBUSY;
1699 }
1700
1701 if (event == CPUFREQ_GOV_STOP)
1702 policy->governor_enabled = false;
1703 else if (event == CPUFREQ_GOV_START)
1704 policy->governor_enabled = true;
1705
1706 mutex_unlock(&cpufreq_governor_lock);
1707
1da177e4
LT
1708 ret = policy->governor->governor(policy, event);
1709
4d5dcc42
VK
1710 if (!ret) {
1711 if (event == CPUFREQ_GOV_POLICY_INIT)
1712 policy->governor->initialized++;
1713 else if (event == CPUFREQ_GOV_POLICY_EXIT)
1714 policy->governor->initialized--;
95731ebb
XC
1715 } else {
1716 /* Restore original values */
1717 mutex_lock(&cpufreq_governor_lock);
1718 if (event == CPUFREQ_GOV_STOP)
1719 policy->governor_enabled = true;
1720 else if (event == CPUFREQ_GOV_START)
1721 policy->governor_enabled = false;
1722 mutex_unlock(&cpufreq_governor_lock);
4d5dcc42 1723 }
b394058f 1724
fe492f3f
VK
1725 if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
1726 ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
1da177e4
LT
1727 module_put(policy->governor->owner);
1728
1729 return ret;
1730}
1731
1da177e4
LT
1732int cpufreq_register_governor(struct cpufreq_governor *governor)
1733{
3bcb09a3 1734 int err;
1da177e4
LT
1735
1736 if (!governor)
1737 return -EINVAL;
1738
a7b422cd
KRW
1739 if (cpufreq_disabled())
1740 return -ENODEV;
1741
3fc54d37 1742 mutex_lock(&cpufreq_governor_mutex);
32ee8c3e 1743
b394058f 1744 governor->initialized = 0;
3bcb09a3
JF
1745 err = -EBUSY;
1746 if (__find_governor(governor->name) == NULL) {
1747 err = 0;
1748 list_add(&governor->governor_list, &cpufreq_governor_list);
1da177e4 1749 }
1da177e4 1750
32ee8c3e 1751 mutex_unlock(&cpufreq_governor_mutex);
3bcb09a3 1752 return err;
1da177e4
LT
1753}
1754EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1755
1da177e4
LT
1756void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1757{
90e41bac
PB
1758#ifdef CONFIG_HOTPLUG_CPU
1759 int cpu;
1760#endif
1761
1da177e4
LT
1762 if (!governor)
1763 return;
1764
a7b422cd
KRW
1765 if (cpufreq_disabled())
1766 return;
1767
90e41bac
PB
1768#ifdef CONFIG_HOTPLUG_CPU
1769 for_each_present_cpu(cpu) {
1770 if (cpu_online(cpu))
1771 continue;
1772 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1773 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1774 }
1775#endif
1776
3fc54d37 1777 mutex_lock(&cpufreq_governor_mutex);
1da177e4 1778 list_del(&governor->governor_list);
3fc54d37 1779 mutex_unlock(&cpufreq_governor_mutex);
1da177e4
LT
1780 return;
1781}
1782EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1783
1784
1da177e4
LT
1785/*********************************************************************
1786 * POLICY INTERFACE *
1787 *********************************************************************/
1788
1789/**
1790 * cpufreq_get_policy - get the current cpufreq_policy
29464f28
DJ
1791 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1792 * is written
1da177e4
LT
1793 *
1794 * Reads the current cpufreq policy.
1795 */
1796int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1797{
1798 struct cpufreq_policy *cpu_policy;
1799 if (!policy)
1800 return -EINVAL;
1801
1802 cpu_policy = cpufreq_cpu_get(cpu);
1803 if (!cpu_policy)
1804 return -EINVAL;
1805
d5b73cd8 1806 memcpy(policy, cpu_policy, sizeof(*policy));
1da177e4
LT
1807
1808 cpufreq_cpu_put(cpu_policy);
1da177e4
LT
1809 return 0;
1810}
1811EXPORT_SYMBOL(cpufreq_get_policy);
1812
153d7f3f 1813/*
e08f5f5b
GS
1814 * data : current policy.
1815 * policy : policy to be set.
153d7f3f 1816 */
3a3e9e06
VK
1817static int __cpufreq_set_policy(struct cpufreq_policy *policy,
1818 struct cpufreq_policy *new_policy)
1da177e4 1819{
7bd353a9 1820 int ret = 0, failed = 1;
1da177e4 1821
3a3e9e06
VK
1822 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", new_policy->cpu,
1823 new_policy->min, new_policy->max);
1da177e4 1824
d5b73cd8 1825 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
1da177e4 1826
3a3e9e06 1827 if (new_policy->min > policy->max || new_policy->max < policy->min) {
9c9a43ed
MD
1828 ret = -EINVAL;
1829 goto error_out;
1830 }
1831
1da177e4 1832 /* verify the cpu speed can be set within this limit */
3a3e9e06 1833 ret = cpufreq_driver->verify(new_policy);
1da177e4
LT
1834 if (ret)
1835 goto error_out;
1836
1da177e4 1837 /* adjust if necessary - all reasons */
e041c683 1838 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
3a3e9e06 1839 CPUFREQ_ADJUST, new_policy);
1da177e4
LT
1840
1841 /* adjust if necessary - hardware incompatibility*/
e041c683 1842 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
3a3e9e06 1843 CPUFREQ_INCOMPATIBLE, new_policy);
1da177e4 1844
bb176f7d
VK
1845 /*
1846 * verify the cpu speed can be set within this limit, which might be
1847 * different to the first one
1848 */
3a3e9e06 1849 ret = cpufreq_driver->verify(new_policy);
e041c683 1850 if (ret)
1da177e4 1851 goto error_out;
1da177e4
LT
1852
1853 /* notification of the new policy */
e041c683 1854 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
3a3e9e06 1855 CPUFREQ_NOTIFY, new_policy);
1da177e4 1856
3a3e9e06
VK
1857 policy->min = new_policy->min;
1858 policy->max = new_policy->max;
1da177e4 1859
2d06d8c4 1860 pr_debug("new min and max freqs are %u - %u kHz\n",
3a3e9e06 1861 policy->min, policy->max);
1da177e4 1862
1c3d85dd 1863 if (cpufreq_driver->setpolicy) {
3a3e9e06 1864 policy->policy = new_policy->policy;
2d06d8c4 1865 pr_debug("setting range\n");
3a3e9e06 1866 ret = cpufreq_driver->setpolicy(new_policy);
1da177e4 1867 } else {
3a3e9e06 1868 if (new_policy->governor != policy->governor) {
1da177e4 1869 /* save old, working values */
3a3e9e06 1870 struct cpufreq_governor *old_gov = policy->governor;
1da177e4 1871
2d06d8c4 1872 pr_debug("governor switch\n");
1da177e4
LT
1873
1874 /* end old governor */
3a3e9e06
VK
1875 if (policy->governor) {
1876 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1877 unlock_policy_rwsem_write(new_policy->cpu);
1878 __cpufreq_governor(policy,
7bd353a9 1879 CPUFREQ_GOV_POLICY_EXIT);
3a3e9e06 1880 lock_policy_rwsem_write(new_policy->cpu);
7bd353a9 1881 }
1da177e4
LT
1882
1883 /* start new governor */
3a3e9e06
VK
1884 policy->governor = new_policy->governor;
1885 if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
1886 if (!__cpufreq_governor(policy, CPUFREQ_GOV_START)) {
7bd353a9 1887 failed = 0;
955ef483 1888 } else {
3a3e9e06
VK
1889 unlock_policy_rwsem_write(new_policy->cpu);
1890 __cpufreq_governor(policy,
7bd353a9 1891 CPUFREQ_GOV_POLICY_EXIT);
3a3e9e06 1892 lock_policy_rwsem_write(new_policy->cpu);
955ef483 1893 }
7bd353a9
VK
1894 }
1895
1896 if (failed) {
1da177e4 1897 /* new governor failed, so re-start old one */
2d06d8c4 1898 pr_debug("starting governor %s failed\n",
3a3e9e06 1899 policy->governor->name);
1da177e4 1900 if (old_gov) {
3a3e9e06
VK
1901 policy->governor = old_gov;
1902 __cpufreq_governor(policy,
7bd353a9 1903 CPUFREQ_GOV_POLICY_INIT);
3a3e9e06 1904 __cpufreq_governor(policy,
e08f5f5b 1905 CPUFREQ_GOV_START);
1da177e4
LT
1906 }
1907 ret = -EINVAL;
1908 goto error_out;
1909 }
1910 /* might be a policy change, too, so fall through */
1911 }
2d06d8c4 1912 pr_debug("governor: change or update limits\n");
3de9bdeb 1913 ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
1da177e4
LT
1914 }
1915
7d5e350f 1916error_out:
1da177e4
LT
1917 return ret;
1918}
1919
1da177e4
LT
1920/**
1921 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1922 * @cpu: CPU which shall be re-evaluated
1923 *
25985edc 1924 * Useful for policy notifiers which have different necessities
1da177e4
LT
1925 * at different times.
1926 */
1927int cpufreq_update_policy(unsigned int cpu)
1928{
3a3e9e06
VK
1929 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1930 struct cpufreq_policy new_policy;
f1829e4a 1931 int ret;
1da177e4 1932
3a3e9e06 1933 if (!policy) {
f1829e4a
JL
1934 ret = -ENODEV;
1935 goto no_policy;
1936 }
1da177e4 1937
f1829e4a
JL
1938 if (unlikely(lock_policy_rwsem_write(cpu))) {
1939 ret = -EINVAL;
1940 goto fail;
1941 }
1da177e4 1942
2d06d8c4 1943 pr_debug("updating policy for CPU %u\n", cpu);
d5b73cd8 1944 memcpy(&new_policy, policy, sizeof(*policy));
3a3e9e06
VK
1945 new_policy.min = policy->user_policy.min;
1946 new_policy.max = policy->user_policy.max;
1947 new_policy.policy = policy->user_policy.policy;
1948 new_policy.governor = policy->user_policy.governor;
1da177e4 1949
bb176f7d
VK
1950 /*
1951 * BIOS might change freq behind our back
1952 * -> ask driver for current freq and notify governors about a change
1953 */
1c3d85dd 1954 if (cpufreq_driver->get) {
3a3e9e06
VK
1955 new_policy.cur = cpufreq_driver->get(cpu);
1956 if (!policy->cur) {
2d06d8c4 1957 pr_debug("Driver did not initialize current freq");
3a3e9e06 1958 policy->cur = new_policy.cur;
a85f7bd3 1959 } else {
3a3e9e06
VK
1960 if (policy->cur != new_policy.cur && cpufreq_driver->target)
1961 cpufreq_out_of_sync(cpu, policy->cur,
1962 new_policy.cur);
a85f7bd3 1963 }
0961dd0d
TR
1964 }
1965
3a3e9e06 1966 ret = __cpufreq_set_policy(policy, &new_policy);
1da177e4 1967
5a01f2e8
VP
1968 unlock_policy_rwsem_write(cpu);
1969
f1829e4a 1970fail:
3a3e9e06 1971 cpufreq_cpu_put(policy);
f1829e4a 1972no_policy:
1da177e4
LT
1973 return ret;
1974}
1975EXPORT_SYMBOL(cpufreq_update_policy);
1976
2760984f 1977static int cpufreq_cpu_callback(struct notifier_block *nfb,
c32b6b8e
AR
1978 unsigned long action, void *hcpu)
1979{
1980 unsigned int cpu = (unsigned long)hcpu;
8a25a2fd 1981 struct device *dev;
5302c3fb 1982 bool frozen = false;
c32b6b8e 1983
8a25a2fd
KS
1984 dev = get_cpu_device(cpu);
1985 if (dev) {
5302c3fb
SB
1986
1987 if (action & CPU_TASKS_FROZEN)
1988 frozen = true;
1989
1990 switch (action & ~CPU_TASKS_FROZEN) {
c32b6b8e 1991 case CPU_ONLINE:
5302c3fb 1992 __cpufreq_add_dev(dev, NULL, frozen);
23d32899 1993 cpufreq_update_policy(cpu);
c32b6b8e 1994 break;
5302c3fb 1995
c32b6b8e 1996 case CPU_DOWN_PREPARE:
5302c3fb 1997 __cpufreq_remove_dev(dev, NULL, frozen);
c32b6b8e 1998 break;
5302c3fb 1999
5a01f2e8 2000 case CPU_DOWN_FAILED:
5302c3fb 2001 __cpufreq_add_dev(dev, NULL, frozen);
c32b6b8e
AR
2002 break;
2003 }
2004 }
2005 return NOTIFY_OK;
2006}
2007
9c36f746 2008static struct notifier_block __refdata cpufreq_cpu_notifier = {
bb176f7d 2009 .notifier_call = cpufreq_cpu_callback,
c32b6b8e 2010};
1da177e4
LT
2011
2012/*********************************************************************
2013 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2014 *********************************************************************/
2015
2016/**
2017 * cpufreq_register_driver - register a CPU Frequency driver
2018 * @driver_data: A struct cpufreq_driver containing the values#
2019 * submitted by the CPU Frequency driver.
2020 *
bb176f7d 2021 * Registers a CPU Frequency driver to this core code. This code
1da177e4 2022 * returns zero on success, -EBUSY when another driver got here first
32ee8c3e 2023 * (and isn't unregistered in the meantime).
1da177e4
LT
2024 *
2025 */
221dee28 2026int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1da177e4
LT
2027{
2028 unsigned long flags;
2029 int ret;
2030
a7b422cd
KRW
2031 if (cpufreq_disabled())
2032 return -ENODEV;
2033
1da177e4
LT
2034 if (!driver_data || !driver_data->verify || !driver_data->init ||
2035 ((!driver_data->setpolicy) && (!driver_data->target)))
2036 return -EINVAL;
2037
2d06d8c4 2038 pr_debug("trying to register driver %s\n", driver_data->name);
1da177e4
LT
2039
2040 if (driver_data->setpolicy)
2041 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2042
0d1857a1 2043 write_lock_irqsave(&cpufreq_driver_lock, flags);
1c3d85dd 2044 if (cpufreq_driver) {
0d1857a1 2045 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1da177e4
LT
2046 return -EBUSY;
2047 }
1c3d85dd 2048 cpufreq_driver = driver_data;
0d1857a1 2049 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1da177e4 2050
8a25a2fd 2051 ret = subsys_interface_register(&cpufreq_interface);
8f5bc2ab
JS
2052 if (ret)
2053 goto err_null_driver;
1da177e4 2054
1c3d85dd 2055 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1da177e4
LT
2056 int i;
2057 ret = -ENODEV;
2058
2059 /* check for at least one working CPU */
7a6aedfa
MT
2060 for (i = 0; i < nr_cpu_ids; i++)
2061 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
1da177e4 2062 ret = 0;
7a6aedfa
MT
2063 break;
2064 }
1da177e4
LT
2065
2066 /* if all ->init() calls failed, unregister */
2067 if (ret) {
2d06d8c4 2068 pr_debug("no CPU initialized for driver %s\n",
e08f5f5b 2069 driver_data->name);
8a25a2fd 2070 goto err_if_unreg;
1da177e4
LT
2071 }
2072 }
2073
8f5bc2ab 2074 register_hotcpu_notifier(&cpufreq_cpu_notifier);
2d06d8c4 2075 pr_debug("driver %s up and running\n", driver_data->name);
1da177e4 2076
8f5bc2ab 2077 return 0;
8a25a2fd
KS
2078err_if_unreg:
2079 subsys_interface_unregister(&cpufreq_interface);
8f5bc2ab 2080err_null_driver:
0d1857a1 2081 write_lock_irqsave(&cpufreq_driver_lock, flags);
1c3d85dd 2082 cpufreq_driver = NULL;
0d1857a1 2083 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
4d34a67d 2084 return ret;
1da177e4
LT
2085}
2086EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2087
1da177e4
LT
2088/**
2089 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2090 *
bb176f7d 2091 * Unregister the current CPUFreq driver. Only call this if you have
1da177e4
LT
2092 * the right to do so, i.e. if you have succeeded in initialising before!
2093 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2094 * currently not initialised.
2095 */
221dee28 2096int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1da177e4
LT
2097{
2098 unsigned long flags;
2099
1c3d85dd 2100 if (!cpufreq_driver || (driver != cpufreq_driver))
1da177e4 2101 return -EINVAL;
1da177e4 2102
2d06d8c4 2103 pr_debug("unregistering driver %s\n", driver->name);
1da177e4 2104
8a25a2fd 2105 subsys_interface_unregister(&cpufreq_interface);
65edc68c 2106 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
1da177e4 2107
6eed9404 2108 down_write(&cpufreq_rwsem);
0d1857a1 2109 write_lock_irqsave(&cpufreq_driver_lock, flags);
6eed9404 2110
1c3d85dd 2111 cpufreq_driver = NULL;
6eed9404 2112
0d1857a1 2113 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
6eed9404 2114 up_write(&cpufreq_rwsem);
1da177e4
LT
2115
2116 return 0;
2117}
2118EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
5a01f2e8
VP
2119
2120static int __init cpufreq_core_init(void)
2121{
2122 int cpu;
2123
a7b422cd
KRW
2124 if (cpufreq_disabled())
2125 return -ENODEV;
2126
474deff7 2127 for_each_possible_cpu(cpu)
5a01f2e8 2128 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
8aa84ad8 2129
2361be23 2130 cpufreq_global_kobject = kobject_create();
8aa84ad8 2131 BUG_ON(!cpufreq_global_kobject);
e00e56df 2132 register_syscore_ops(&cpufreq_syscore_ops);
8aa84ad8 2133
5a01f2e8
VP
2134 return 0;
2135}
5a01f2e8 2136core_initcall(cpufreq_core_init);
This page took 1.24178 seconds and 5 git commands to generate.