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