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