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