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